| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::Hash - Core data class for XAO::FS | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $data=XAO::Objects->new(objname => 'FS::Hash'); | ||
| 8 | |||
| 9 | $data->put(aaa => 123); | ||
| 10 | |||
| 11 | my $aaa=$data->get('aaa'); | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This is a core data class for XAO Foundation Server (see | ||
| 16 | L<XAO::FS>). All data classes are based on FS::Hash and for that | ||
| 17 | matter FS::Hash can be considered pure virtual class which you would | ||
| 18 | never want to use directly. | ||
| 19 | |||
| 20 | A data object can contain arbitrary number of named single-value | ||
| 21 | parameters and arbitrary number of list objects. Whenever you need more | ||
| 22 | then one count of something you will have to create a list object to | ||
| 23 | store those things. For example if you only have one shipping address | ||
| 24 | per customer - you can store it as a couple of properties in Customer | ||
| 25 | object, but if you want a customer to have an address book - create list | ||
| 26 | object named Addresses and store addresses inside of it. | ||
| 27 | |||
| 28 | Any data object at any given time can be in either attached state | ||
| 29 | (stored in some List and connected to the database) or in detached state | ||
| 30 | (when it is not connected to the database layer all manipulations on | ||
| 31 | object's properties only change memory). | ||
| 32 | |||
| 33 | Detached data object can be at any time re-attached to a container or | ||
| 34 | stored under different ID. That allows a developer to have increased | ||
| 35 | performance when required by detaching an object and then using | ||
| 36 | in-memory copy. | ||
| 37 | |||
| 38 | When an object is attached all data manipulations are done directly | ||
| 39 | on database and never cached. Reasonable measures should be taken by | ||
| 40 | developer to ensure that it is safe to re-attach an object to the | ||
| 41 | database because the content of the object would replace current | ||
| 42 | database content. Object server does not try to map any changes that you | ||
| 43 | make to the database back to the detached object. Once detached the | ||
| 44 | object is on its own. | ||
| 45 | |||
| 46 | Here is the entire API that is available in all objects based on | ||
| 47 | the FS::Hash class. It is not recommended to override | ||
| 48 | or extend any of these methods unless stated otherwise in method | ||
| 49 | description. Methods are listed in alphabetical order. | ||
| 50 | |||
| 51 | =over | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | ############################################################################### | ||
| 56 | # This is not a displayable object and it does not have a display() | ||
| 57 | # method.. | ||
| 58 | # | ||
| 59 | # Making the object a two-fold - both data manipulation and displayable | ||
| 60 | # would be a nice feature to have later though.. | ||
| 61 | # | ||
| 62 | package XAO::DO::FS::Hash; | ||
| 63 | use strict; | ||
| 64 | use utf8; | ||
| 65 | use XAO::Utils; | ||
| 66 | use XAO::Objects; | ||
| 67 | use Encode; | ||
| 68 | |||
| 69 | use base XAO::Objects->load(objname => 'FS::Glue'); | ||
| 70 | |||
| 71 | use vars qw($VERSION); | ||
| 72 | $VERSION=(0+sprintf('%u.%03u',(q$Id: Hash.pm,v 2.10 2008/09/14 07:07:12 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; | ||
| 73 | |||
| 74 | ############################################################################### | ||
| 75 | |||
| 76 | =item add_placeholder (%) | ||
| 77 | |||
| 78 | Modifies database scheme by adding new possible parameter name into the | ||
| 79 | object. After you call this method you can call put() and get() on the | ||
| 80 | name you have added. | ||
| 81 | |||
| 82 | The operation can take some time especially if you have a lot of objects | ||
| 83 | of that type in the database because it have to modify database tables | ||
| 84 | and add new field. | ||
| 85 | |||
| 86 | Arguments are (alphabetically): | ||
| 87 | |||
| 88 | =over | ||
| 89 | |||
| 90 | =item class | ||
| 91 | |||
| 92 | Only makes sense for 'list' type - sets the name of class for the | ||
| 93 | objects that would be contained in the list. That class have to exist | ||
| 94 | and be available for object loader at the time of the call. | ||
| 95 | |||
| 96 | =item charset | ||
| 97 | |||
| 98 | For fields of 'text' type this can be one of 'binary', 'latin1', or | ||
| 99 | 'utf8'. For compatibility with older version that did not have a | ||
| 100 | dedicated 'blob' data type the default charset is 'binary'. | ||
| 101 | |||
| 102 | Fields of 'latin1' or 'utf8' charset have no guaranteed behaviour on | ||
| 103 | characters not defined for these charsets. Such characters can be | ||
| 104 | skipped or converted to '?' for instance. | ||
| 105 | |||
| 106 | 'Binary' charset fields will accept any data just like 'blob' data type, | ||
| 107 | but the sorting order and word matching in search may differ from older | ||
| 108 | versions. As another side effect, searches on 'binary' charset fields | ||
| 109 | are case sensitive. | ||
| 110 | |||
| 111 | B<NOTE:> The 'utf8' charset only supports the Basic Multilingual Plane | ||
| 112 | characters (0x0000-0xffff). These characters in UTF-8 encoding are | ||
| 113 | guaranteed to take up from 1 to 3 bytes. Supplemental Unicode planes are | ||
| 114 | not supported. | ||
| 115 | |||
| 116 | =item connector | ||
| 117 | |||
| 118 | Optional name of the key that would refer objects in the list to the | ||
| 119 | object they are contained in. Default is 'parent_unique_id', you can | ||
| 120 | change it to something more meaningfull so that it would make sense for | ||
| 121 | somebody looking at the plain SQL tables. | ||
| 122 | |||
| 123 | =item default | ||
| 124 | |||
| 125 | Sets default values that would be returned from get() when no content is | ||
| 126 | available. Valid only for data types. Defaults to 0 or minvalue if zero | ||
| 127 | is not in the range for 'real' and 'integer' types and empty string for | ||
| 128 | 'text'. | ||
| 129 | |||
| 130 | Maximum length of the default text is limited to 30 characters | ||
| 131 | regardless of the 'maxlength'. | ||
| 132 | |||
| 133 | =item index | ||
| 134 | |||
| 135 | Optional parameter that when set to non-zero value suggests that you are | ||
| 136 | going to use this field in searches a lot. This works especially good on | ||
| 137 | `real' and `integer' fields when you later search on them using ranges | ||
| 138 | or equivalence. | ||
| 139 | |||
| 140 | Note, that indexing text fields works mostly on 'eq', 'ne', and 'sw' | ||
| 141 | search operators, not 'cs', 'ws', or 'wq'. | ||
| 142 | |||
| 143 | Regardless of your use of indexes searches are guaranteed to produce | ||
| 144 | equal results. Indexing can only improve performance (or decrease it when | ||
| 145 | used incorrectly). | ||
| 146 | |||
| 147 | =item key | ||
| 148 | |||
| 149 | Name of the key that would identify objects in the list. Required if | ||
| 150 | you add a 'list' placeholder. | ||
| 151 | |||
| 152 | When possible it is recommended to name the key as the class name or | ||
| 153 | some derivative from it with '_id' suffix. If you add Orders list | ||
| 154 | placeholder it is recommended to call the key 'order_id'. | ||
| 155 | |||
| 156 | =item key_charset | ||
| 157 | |||
| 158 | Charset to be used for the list key. Default is 'binary' which should be | ||
| 159 | sufficient for the majority of applications. | ||
| 160 | |||
| 161 | =item key_format | ||
| 162 | |||
| 163 | Only for 'list' placeholders. Specifies format of the auto-generated | ||
| 164 | keys for a single argument put() method on Lists (L<XAO::DO::FS::List>). | ||
| 165 | |||
| 166 | Key format consists of normal alpha-numeric characters and may include | ||
| 167 | several special sequences: | ||
| 168 | |||
| 169 | =over | ||
| 170 | |||
| 171 | =item <$RANDOM$> | ||
| 172 | |||
| 173 | Random 8-character sequence as generated by XAO::Utils::generate_key() | ||
| 174 | function. It never evaluates to 'false' in perl sense, always starts | ||
| 175 | from a letter and has some other constraints - see L<XAO::Utils> for | ||
| 176 | details. | ||
| 177 | |||
| 178 | =item <$AUTOINC$> | ||
| 179 | |||
| 180 | This is an auto-incrementing unsigned at least 32-bit integer value. It | ||
| 181 | is not guaranteed to be continuously incrementing, but new value is | ||
| 182 | always greater then any previously assigned and greater then zero. There | ||
| 183 | is no definition of what would happen if all integer values are used up. | ||
| 184 | |||
| 185 | Can optionally have number of digits specified - <$AUTOINC/10$> to | ||
| 186 | generate 10 digits integers. Default is to use minimum possible number | ||
| 187 | of digits. | ||
| 188 | |||
| 189 | =item <$GMTIME$> | ||
| 190 | |||
| 191 | Seconds GMT since Epoch. | ||
| 192 | |||
| 193 | =item <$DATE$> | ||
| 194 | |||
| 195 | Current date and time in YYYYMMDDHHMMSS format. | ||
| 196 | |||
| 197 | =back | ||
| 198 | |||
| 199 | One of <$AUTOINC$> or <$RANDOM$> must present in the key format as | ||
| 200 | otherwise it is impossible to guarantee key uniqueness. | ||
| 201 | |||
| 202 | The default key format is '<$RANDOM$>'. | ||
| 203 | |||
| 204 | Examples: | ||
| 205 | |||
| 206 | V<$AUTOINC$> - V1, V2, V3, ..., V12345 | ||
| 207 | <$DATE$>_<$RANDOM$> - 200210282218_QWERT123 | ||
| 208 | NUM<$AUTOINC/12$> - NUM000000000001, ..., NUM000000012345 | ||
| 209 | |||
| 210 | =item key_length | ||
| 211 | |||
| 212 | Maximum allowed key length, default is 30 (the same as maximum field | ||
| 213 | name length in Hashes for consistency reasons). | ||
| 214 | |||
| 215 | =item maxlength | ||
| 216 | |||
| 217 | Maximum length for 'text' type in bytes (so for 'utf8' charset you may | ||
| 218 | store less characters than the maxlength depending on the data). If | ||
| 219 | later on you will try to store longer text string into that field an | ||
| 220 | error will be thrown. | ||
| 221 | |||
| 222 | Default is 100, but using default length is deprecated -- it may become | ||
| 223 | illegal in future versions. | ||
| 224 | |||
| 225 | =item maxvalue | ||
| 226 | |||
| 227 | Maximum possible value for `integer' and `real' properties, | ||
| 228 | inclusive. Default is 2147483647 for integer property if minvalue is | ||
| 229 | negative and 4294967295 if minvalue is zero or positive. | ||
| 230 | |||
| 231 | Depending on database driver used you may inmprove performance when you | ||
| 232 | use minimal possible ranges for your values. This is true for MySQL at | ||
| 233 | least. | ||
| 234 | |||
| 235 | =item minvalue | ||
| 236 | |||
| 237 | Minimum possible value for `integer' and `real' properties, | ||
| 238 | inclusive. Default is -2147483648 for integer property. If you use | ||
| 239 | positive value or zero for integer property you're converting that | ||
| 240 | property to unsigned integer value effectively. | ||
| 241 | |||
| 242 | =item scale | ||
| 243 | |||
| 244 | Only makes sense for 'real' type. When set the field is stored as a | ||
| 245 | fixed precision decimal with guaranteed math properties (as opposed to | ||
| 246 | plain 'real' values where there may not be a way to store exactly 3.0 | ||
| 247 | for example). | ||
| 248 | |||
| 249 | =item name | ||
| 250 | |||
| 251 | Placeholder name -- this is the name you would then use in put() and | ||
| 252 | get() to access that property. | ||
| 253 | |||
| 254 | It is recommended to name single-value properties in all-lowercase with | ||
| 255 | underscores to separate words (like "middle_name") and name lists in | ||
| 256 | capitalized words (like "Addresses"). Names have to start from letter | ||
| 257 | and can consist of only letters, digits and underscore symbol. | ||
| 258 | |||
| 259 | =item table | ||
| 260 | |||
| 261 | Optional table name for 'list' type placeholder. If not defined it would | ||
| 262 | be created from class name (something like 'osCustomer_Address' for | ||
| 263 | 'Customer::Address' class). | ||
| 264 | |||
| 265 | =item type | ||
| 266 | |||
| 267 | Placeholder type, one of 'text', 'blob', 'integer', 'unsigned', 'real' | ||
| 268 | or 'list'. | ||
| 269 | |||
| 270 | =item unique | ||
| 271 | |||
| 272 | If set to non-zero value then this property will have to be filled with | ||
| 273 | values that are unique troughout the entire class. Placeholder with that | ||
| 274 | modifier can only be added when there are no objects of that class in | ||
| 275 | the database. | ||
| 276 | |||
| 277 | =back | ||
| 278 | |||
| 279 | An example of adding new single-value property placeholder: | ||
| 280 | |||
| 281 | $customer->add_placeholder(name => 'middle_name', | ||
| 282 | type => 'text', | ||
| 283 | maxlength => 10); | ||
| 284 | |||
| 285 | $customer->put(middlename => 'Jr.'); | ||
| 286 | |||
| 287 | An example of adding new list of properties placeholder: | ||
| 288 | |||
| 289 | $customer->add_placeholder(name => 'Addresses', | ||
| 290 | class => 'Customer::Address', | ||
| 291 | key => 'address_id'); | ||
| 292 | |||
| 293 | my $adlist=$customer->get('Addresses'); | ||
| 294 | |||
| 295 | $adlist->put('addr001' => $address); | ||
| 296 | |||
| 297 | B<NOTE:> A placeholder added to an object in fact adds it to all objects | ||
| 298 | of the same class regardless of their status - attached or detached. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub add_placeholder ($%) { | ||
| 303 | my $self=shift; | ||
| 304 | my $args=get_args(\@_); | ||
| 305 | |||
| 306 | my $name=$args->{'name'} || throw $self 'add_placeholder - no name given'; | ||
| 307 | my $type=$args->{'type'} || throw $self 'add_placeholder - no type given'; | ||
| 308 | |||
| 309 | $self->check_name($name) || | ||
| 310 | throw $self "add_placeholder - bad name ($name)"; | ||
| 311 | |||
| 312 | $self->_field_description($name) && | ||
| 313 | throw $self "add_placeholder - placeholder already exists ($name)"; | ||
| 314 | |||
| 315 | if($type eq 'list') { | ||
| 316 | $self->_add_list_placeholder($args); | ||
| 317 | } else { | ||
| 318 | $self->_add_data_placeholder($args); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | ############################################################################### | ||
| 323 | |||
| 324 | =item build_structure (%) | ||
| 325 | |||
| 326 | Convenience method that checks object structure and adds specified | ||
| 327 | placeholders if they do not currently exist. | ||
| 328 | |||
| 329 | If any existing field has a different description than supplied | ||
| 330 | build_structure() method will adjust the database structure when | ||
| 331 | possible, or throw an error if an automatic adjustment is not supported. | ||
| 332 | |||
| 333 | Upon return, the object will have at least the given fields. It can | ||
| 334 | have some extra fields that were on the object before, but given fields | ||
| 335 | guaranteed to exist and have the same descriptions as provided. The list | ||
| 336 | of extra fields is returned. | ||
| 337 | |||
| 338 | Example: | ||
| 339 | |||
| 340 | $customer->build_structure( | ||
| 341 | Orders => { | ||
| 342 | type => 'list', | ||
| 343 | class => 'Data::Order', | ||
| 344 | key => 'order_id', | ||
| 345 | structure => { | ||
| 346 | total => { | ||
| 347 | type => 'integer', | ||
| 348 | minvalue => 0, | ||
| 349 | }, | ||
| 350 | }, | ||
| 351 | }, | ||
| 352 | first_name => { | ||
| 353 | type => 'text', | ||
| 354 | maxlength => 100 | ||
| 355 | }, | ||
| 356 | last_name => { | ||
| 357 | type => 'text', | ||
| 358 | maxlength => 100 | ||
| 359 | }); | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub build_structure ($%) { | ||
| 364 | my $self=shift; | ||
| 365 | my $args=get_args(\@_); | ||
| 366 | |||
| 367 | $self->_consistency_checked || | ||
| 368 | throw $self "- can't build_structure without 'check_consistency' on loading"; | ||
| 369 | |||
| 370 | $self->_glue->transact_active && | ||
| 371 | throw $self "- structure manipulations don't support transactions"; | ||
| 372 | |||
| 373 | my %changed_charset; | ||
| 374 | my %changed_scale; | ||
| 375 | my %changed_default; | ||
| 376 | my %changed_maxlength; | ||
| 377 | |||
| 378 | my %result=( | ||
| 379 | changed => 0, | ||
| 380 | added => 0, | ||
| 381 | orphans => [], | ||
| 382 | ); | ||
| 383 | |||
| 384 | foreach my $name (keys %$args) { | ||
| 385 | my %ph; | ||
| 386 | @ph{keys %{$args->{$name}}}=values %{$args->{$name}}; | ||
| 387 | $ph{'name'}=$name; | ||
| 388 | |||
| 389 | my $type=$ph{'type'}; | ||
| 390 | |||
| 391 | if($type ne 'list' && !defined $ph{'default'}) { | ||
| 392 | $ph{'default'}//=$self->_field_default($name,\%ph); | ||
| 393 | } | ||
| 394 | |||
| 395 | my $desc=$self->describe($name); | ||
| 396 | if($desc) { | ||
| 397 | while(my ($n,$v)=each %ph) { | ||
| 398 | next if $n eq 'name'; | ||
| 399 | next if $n eq 'structure'; | ||
| 400 | next if $n =~ /^_/; | ||
| 401 | next if !defined $v; | ||
| 402 | |||
| 403 | my $dbv=$desc->{$n}; | ||
| 404 | |||
| 405 | if($n eq 'charset') { | ||
| 406 | $v eq 'binary' || Encode::resolve_alias($v) || | ||
| 407 | throw $self "- new charset '$v' is not supported for field '$name'"; | ||
| 408 | |||
| 409 | if($v ne ($dbv || '')) { | ||
| 410 | $changed_charset{$name}=$v; | ||
| 411 | ++$result{'changed'}; | ||
| 412 | dprint "..changed charset for '$name': '$dbv' => '$v'"; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | elsif($n eq 'scale') { | ||
| 416 | $dbv||=0; | ||
| 417 | |||
| 418 | if($v<$dbv) { | ||
| 419 | throw $self "- new scale '$v' is lower than existing '$dbv' for '$name' (only upwards scale changes are automatic)"; | ||
| 420 | } | ||
| 421 | elsif($v>$dbv) { | ||
| 422 | $changed_scale{$name}=$v; | ||
| 423 | ++$result{'changed'}; | ||
| 424 | dprint "..changed scale for '$name': '$dbv' => '$v'"; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | elsif($n eq 'default') { | ||
| 428 | my $match=$type eq 'text' || $type eq 'blob' ? ($v eq $dbv) : ($v == $dbv && $dbv ne '' && $dbv !~ /[a-z]/i); | ||
| 429 | if(!$match) { | ||
| 430 | $changed_default{$name}=$v; | ||
| 431 | ++$result{'changed'}; | ||
| 432 | dprint "..changed default for '$name': '$dbv' => '$v'"; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | elsif($n eq 'maxlength') { | ||
| 436 | my $match=defined $dbv && $v==$dbv; | ||
| 437 | if(!$match) { | ||
| 438 | $type eq 'text' || $type eq 'blob' || | ||
| 439 | throw $self "- 'maxlength' only makes sense for 'text' and 'blob' fields"; | ||
| 440 | $changed_maxlength{$name}=$v; | ||
| 441 | ++$result{'changed'}; | ||
| 442 | dprint "..changed maxlength for '$name': '$dbv' => '$v'"; | ||
| 443 | } | ||
| 444 | } | ||
| 445 | elsif(!defined $dbv || (defined $dbv && $dbv ne $v)) { | ||
| 446 | throw $self "- structure mismatch, property=$name, ($n,$v) <> ($n,".(defined $dbv ? $dbv : '<undef>').")"; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | } | ||
| 450 | else { | ||
| 451 | $self->add_placeholder(\%ph); | ||
| 452 | ++$result{'added'}; | ||
| 453 | } | ||
| 454 | |||
| 455 | # Building recursive structures | ||
| 456 | # | ||
| 457 | if($ph{'type'} eq 'list' && $ph{'structure'}) { | ||
| 458 | my $ro=XAO::Objects->new(objname => $ph{'class'}, | ||
| 459 | glue => $self->_glue); | ||
| 460 | |||
| 461 | my $res=$ro->build_structure($ph{'structure'}); | ||
| 462 | |||
| 463 | $result{'changed'}+=$res->{'changed'}; | ||
| 464 | $result{'added'}+=$res->{'added'}; | ||
| 465 | push(@{$result{'orphans'}}, @{$res->{'orphans'}}); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | $self->_build_structure_change( | ||
| 470 | changes => \%changed_maxlength, | ||
| 471 | structure => $args, | ||
| 472 | param => 'maxlength', | ||
| 473 | ); | ||
| 474 | |||
| 475 | $self->_build_structure_change( | ||
| 476 | changes => \%changed_charset, | ||
| 477 | structure => $args, | ||
| 478 | param => 'charset', | ||
| 479 | ); | ||
| 480 | |||
| 481 | $self->_build_structure_change( | ||
| 482 | changes => \%changed_scale, | ||
| 483 | structure => $args, | ||
| 484 | param => 'scale', | ||
| 485 | ); | ||
| 486 | |||
| 487 | $self->_build_structure_change( | ||
| 488 | changes => \%changed_default, | ||
| 489 | structure => $args, | ||
| 490 | param => 'default', | ||
| 491 | ); | ||
| 492 | |||
| 493 | # Checking for fields that are defined in the database structure, | ||
| 494 | # but not in the given description. | ||
| 495 | # | ||
| 496 | foreach my $k ($self->keys) { | ||
| 497 | next if $args->{$k}; | ||
| 498 | |||
| 499 | my $d=$self->describe($k); | ||
| 500 | |||
| 501 | next if $d->{'type'} eq 'key'; | ||
| 502 | |||
| 503 | # /project is hard-coded and is not usually a part of any given | ||
| 504 | # structure, keeping it. | ||
| 505 | # | ||
| 506 | my $objname=$self->objname; | ||
| 507 | next if $objname eq 'FS::Global' && $k eq 'project'; | ||
| 508 | |||
| 509 | push(@{$result{'orphans'}},{ | ||
| 510 | class => $objname, | ||
| 511 | field => $k, | ||
| 512 | }); | ||
| 513 | } | ||
| 514 | |||
| 515 | return \%result; | ||
| 516 | } | ||
| 517 | |||
| 518 | ############################################################################### | ||
| 519 | |||
| 520 | sub _build_structure_change($@) { | ||
| 521 | my $self=shift; | ||
| 522 | my $args=get_args(\@_); | ||
| 523 | |||
| 524 | my $changes=$args->{'changes'}; | ||
| 525 | my $structure=$args->{'structure'}; | ||
| 526 | my $param=$args->{'param'}; | ||
| 527 | |||
| 528 | return unless $changes && %$changes; | ||
| 529 | |||
| 530 | my $notice=$args->{'notice'}; | ||
| 531 | if(!$notice && $param) { | ||
| 532 | $notice="Some '$param' values in ".$$self->{'class'}." have changed (".join(', ',sort keys %$changes).")."; | ||
| 533 | } | ||
| 534 | |||
| 535 | my $code=$args->{'code'}; | ||
| 536 | if(!$code) { | ||
| 537 | $param || throw $self "- no code and no param"; | ||
| 538 | $code=$self->can('_'.$param.'_change') || | ||
| 539 | throw $self "- no _${param}_change implementation"; | ||
| 540 | } | ||
| 541 | |||
| 542 | # Convenience to avoid waiting | ||
| 543 | # | ||
| 544 | my $forced=grep { $structure->{$_}->{'_force'} } keys %$changes; | ||
| 545 | |||
| 546 | # Applying the changes | ||
| 547 | # | ||
| 548 | my $debug_status=XAO::Utils::get_debug(); | ||
| 549 | if(!$forced) { | ||
| 550 | XAO::Utils::set_debug(1); | ||
| 551 | |||
| 552 | dprint $notice if $notice; | ||
| 553 | dprint "An automatic conversion will be attempted in 10 seconds. Interrupt to abort."; | ||
| 554 | sleep 10; | ||
| 555 | dprint "Converting..."; | ||
| 556 | } | ||
| 557 | |||
| 558 | $code->($self,$changes); | ||
| 559 | |||
| 560 | if(!$forced) { | ||
| 561 | XAO::Utils::set_debug($debug_status); | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | ############################################################################### | ||
| 566 | |||
| 567 | =item sync_structure (%) | ||
| 568 | |||
| 569 | Build the given structure (similarly to L<build_structure()>) and drop all | ||
| 570 | fields that are not mentioned in the given structure definition. | ||
| 571 | |||
| 572 | B<DANGER:> This routine can irreversibly delete table fields and whole | ||
| 573 | tables. | ||
| 574 | |||
| 575 | =cut | ||
| 576 | |||
| 577 | sub sync_structure ($%) { | ||
| 578 | my $self=shift; | ||
| 579 | my $args=get_args(\@_); | ||
| 580 | |||
| 581 | my $res=$self->build_structure($args); | ||
| 582 | |||
| 583 | my $orphans=$res->{'orphans'}; | ||
| 584 | |||
| 585 | if(@$orphans) { | ||
| 586 | my $forced=grep { $args->{$_}->{'_force'} } keys %$args; | ||
| 587 | |||
| 588 | my $debug_status=XAO::Utils::get_debug(); | ||
| 589 | if(!$forced) { | ||
| 590 | XAO::Utils::set_debug(1); | ||
| 591 | |||
| 592 | dprint "Some placeholders are present in the database, but not in the given structure."; | ||
| 593 | foreach my $orphan (@$orphans) { | ||
| 594 | dprint "..$orphan->{'class'} / $orphan->{'field'}"; | ||
| 595 | } | ||
| 596 | dprint "They will be irrecoverably dropped. Interrupt within 10 seconds to abort."; | ||
| 597 | sleep 10; | ||
| 598 | } | ||
| 599 | |||
| 600 | dprint "Dropping placeholders"; | ||
| 601 | |||
| 602 | foreach my $orphan (@$orphans) { | ||
| 603 | my ($class,$field)=@{$orphan}{'class','field'}; | ||
| 604 | |||
| 605 | dprint "..$class / $field"; | ||
| 606 | |||
| 607 | my $obj=XAO::Objects->new( | ||
| 608 | objname => $class, | ||
| 609 | glue => $self->_glue, | ||
| 610 | ); | ||
| 611 | |||
| 612 | $obj->drop_placeholder($field); | ||
| 613 | } | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | ############################################################################### | ||
| 618 | |||
| 619 | =item collection_key () | ||
| 620 | |||
| 621 | Returns the same ID of the object that would be used be a collection | ||
| 622 | object holding this object. See L<XAO::DO::FS::Collection> and | ||
| 623 | collection() method on the glue object. | ||
| 624 | |||
| 625 | Will return undef on detached objects. | ||
| 626 | |||
| 627 | =cut | ||
| 628 | |||
| 629 | sub collection_key ($) { | ||
| 630 | my $self=shift; | ||
| 631 | return $$self->{'unique_id'}; | ||
| 632 | } | ||
| 633 | |||
| 634 | ############################################################################### | ||
| 635 | |||
| 636 | =item container_key () | ||
| 637 | |||
| 638 | If current object is not on top level returns key that refers to the | ||
| 639 | current object in the container object that contains current object. | ||
| 640 | |||
| 641 | Will return undef if current object was created with "new" and had | ||
| 642 | never been stored anywhere. | ||
| 643 | |||
| 644 | =cut | ||
| 645 | |||
| 646 | # defined in Glue. | ||
| 647 | |||
| 648 | ############################################################################### | ||
| 649 | |||
| 650 | =item container_object () | ||
| 651 | |||
| 652 | If current object is not on top level returns container object that | ||
| 653 | contains current object. For Global object will return `undef'. | ||
| 654 | |||
| 655 | Will throw an error if current object was created with get_new() method | ||
| 656 | or something similar. | ||
| 657 | |||
| 658 | Example: | ||
| 659 | |||
| 660 | my $orders_list=$order->container_object(); | ||
| 661 | |||
| 662 | =cut | ||
| 663 | |||
| 664 | sub container_object ($) { | ||
| 665 | my $self=shift; | ||
| 666 | |||
| 667 | return undef if $self->objname eq 'FS::Global'; | ||
| 668 | |||
| 669 | my $list_base_name=$$self->{'list_base_name'} || | ||
| 670 | $self->throw("container_object - the object was not retrieved from the database"); | ||
| 671 | |||
| 672 | ## | ||
| 673 | # This is an optimisation for the case where current object has an | ||
| 674 | # URI already. It might not have it if container_object() is called | ||
| 675 | # from new() to determine URI in case of Collection. | ||
| 676 | # | ||
| 677 | my $uri=$self->uri; | ||
| 678 | if(defined($uri)) { | ||
| 679 | my @path=split(/\/+/,$uri); | ||
| 680 | $uri=join('/',@path[0..$#path-1]); | ||
| 681 | } | ||
| 682 | |||
| 683 | XAO::Objects->new( | ||
| 684 | objname => 'FS::List', | ||
| 685 | glue => $self->_glue, | ||
| 686 | class_name => $self->objname, | ||
| 687 | base_name => $list_base_name, | ||
| 688 | base_id => $self->_hash_list_base_id(), | ||
| 689 | key_value => $self->_hash_list_key_value(), | ||
| 690 | uri => $uri, | ||
| 691 | ); | ||
| 692 | } | ||
| 693 | |||
| 694 | ############################################################################### | ||
| 695 | |||
| 696 | =item defined ($) | ||
| 697 | |||
| 698 | Obsolete since version 1.03 when the concept of existing, but undefined | ||
| 699 | value was eliminated for simplicity. Values are always defined -- | ||
| 700 | therefore this method will either return true or throw an error if the | ||
| 701 | name is not correct. | ||
| 702 | |||
| 703 | =cut | ||
| 704 | |||
| 705 | sub defined ($$) { | ||
| 706 | my $self=shift; | ||
| 707 | my $name=shift; | ||
| 708 | |||
| 709 | return 1 if $name eq 'unique_id'; | ||
| 710 | |||
| 711 | my $f=$self->_class_description->{'fields'}; | ||
| 712 | |||
| 713 | exists $f->{$name} || | ||
| 714 | throw $self "defined - unknown property ($name)"; | ||
| 715 | } | ||
| 716 | |||
| 717 | ############################################################################### | ||
| 718 | |||
| 719 | =item delete ($) | ||
| 720 | |||
| 721 | Deletes content of the given property. If you use get() on the deleted | ||
| 722 | property you will get empty List for `list' properties or `undef' for | ||
| 723 | value properties. | ||
| 724 | |||
| 725 | B<NOTE:> If the name you gave refers to a contained object then | ||
| 726 | destroy() method would be called on that object. List object would then | ||
| 727 | unlink all its contained object and if that was the only place they were | ||
| 728 | linked into then these object would be destroyed too. Be careful with | ||
| 729 | delete() method. | ||
| 730 | |||
| 731 | Delete() method would not alter database structure. It can leave some | ||
| 732 | tables empty, but it would not change relations scheme. | ||
| 733 | |||
| 734 | =cut | ||
| 735 | |||
| 736 | sub delete ($$) { | ||
| 737 | my $self=shift; | ||
| 738 | my $name=shift; | ||
| 739 | |||
| 740 | my $field=$self->_field_description($name); | ||
| 741 | $field || $self->throw("delete($name) - unknown field name"); | ||
| 742 | |||
| 743 | my $type=$field->{'type'}; | ||
| 744 | if($type eq 'list') { | ||
| 745 | $self->get($name)->destroy(); | ||
| 746 | } | ||
| 747 | elsif($type eq 'connector') { | ||
| 748 | throw $self "delete($name) - deleting connectors is not allowed"; | ||
| 749 | } | ||
| 750 | elsif($name eq 'unique_id') { | ||
| 751 | throw $self "delete($name) - attempt to delete unique_id"; | ||
| 752 | } | ||
| 753 | elsif($type eq 'key') { | ||
| 754 | throw $self "delete($name) - attempt to delete the key"; | ||
| 755 | } | ||
| 756 | else { | ||
| 757 | $self->put($name => undef); | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | ############################################################################### | ||
| 762 | |||
| 763 | =item describe ($) | ||
| 764 | |||
| 765 | Returns a hash reference that contains description of the given | ||
| 766 | field. The format is exactly like you would pass to add_placeholder() | ||
| 767 | method. | ||
| 768 | |||
| 769 | Can be used to check limitations and field types. Will return `undef' on | ||
| 770 | non-existing fields. | ||
| 771 | |||
| 772 | Example: | ||
| 773 | |||
| 774 | my $description=$customer->describe('first_name'); | ||
| 775 | print "Type: $description->{'type'}\n"; | ||
| 776 | print "Maximum length: $description->{'maxlength'}\n"; | ||
| 777 | |||
| 778 | =cut | ||
| 779 | |||
| 780 | sub describe ($$) { | ||
| 781 | my $self=shift; | ||
| 782 | my $name=shift; | ||
| 783 | my $desc=$self->_field_description($name); | ||
| 784 | return undef unless $desc; | ||
| 785 | |||
| 786 | # We copy description so that whoever needs it, would not modify our | ||
| 787 | # internal structure. | ||
| 788 | # | ||
| 789 | my %d; | ||
| 790 | @d{keys %{$desc}}=values %{$desc}; | ||
| 791 | \%d; | ||
| 792 | } | ||
| 793 | |||
| 794 | ############################################################################### | ||
| 795 | |||
| 796 | =item destroy () | ||
| 797 | |||
| 798 | Deletes everything inside the current object -- an alias for the | ||
| 799 | following code: | ||
| 800 | |||
| 801 | foreach my $key ($customer->keys) { | ||
| 802 | $customer->delete($key); | ||
| 803 | } | ||
| 804 | |||
| 805 | =cut | ||
| 806 | |||
| 807 | # Implemented in Glue.pm | ||
| 808 | |||
| 809 | ############################################################################### | ||
| 810 | |||
| 811 | =item detach () | ||
| 812 | |||
| 813 | Detaches current object from its container and from database. Detaching | ||
| 814 | an object leads to detaching every object it contains to the deepest | ||
| 815 | possible level. | ||
| 816 | |||
| 817 | Once an object is detached it "remembers" a place where it was attached | ||
| 818 | and can be re-attached later. | ||
| 819 | |||
| 820 | No changes in the database data would be propagated to the detached | ||
| 821 | object and no changes in the detached object would ever change the | ||
| 822 | database. An exception is add_placeholder() and drop_placeholder() | ||
| 823 | methods that operate on all objects of the same type regardless of their | ||
| 824 | status -- detached or attached. | ||
| 825 | |||
| 826 | B<NOT IMPLEMENTED YET>. Almost all supporting infrastructure is in | ||
| 827 | place, but currently the only way to get a detached object is to call | ||
| 828 | get_new() on List object. | ||
| 829 | |||
| 830 | B<It is safe to call detach() though.> You should do that in places | ||
| 831 | where you think this is appropriate. Like that: | ||
| 832 | |||
| 833 | ## | ||
| 834 | # Printing all properties. Detach() will load all the values into | ||
| 835 | # memory and allow speedy prints. The code will work in exactly the | ||
| 836 | # same way with or without detach() but once detach() is implemented | ||
| 837 | # there would be speed up. | ||
| 838 | # | ||
| 839 | my $obj=$customers->get($customer_id); | ||
| 840 | $obj->detach(); | ||
| 841 | foreach my $key ($obj->keys) { | ||
| 842 | my $value=$obj->get($key); | ||
| 843 | if(ref($value)) { | ||
| 844 | print "obj{$key}=List (" . $value->uri . ")\n"; | ||
| 845 | } | ||
| 846 | else { | ||
| 847 | print "obj{$key}='$value'\n"; | ||
| 848 | } | ||
| 849 | } | ||
| 850 | |||
| 851 | =cut | ||
| 852 | |||
| 853 | sub detach ($) { | ||
| 854 | # Nothing | ||
| 855 | } | ||
| 856 | |||
| 857 | ############################################################################### | ||
| 858 | |||
| 859 | =item drop_placeholder ($) | ||
| 860 | |||
| 861 | Deletes placeholder and all values stored in the field being deleted in | ||
| 862 | all Hash objects. Be careful! | ||
| 863 | |||
| 864 | It might take considerable amount of time to finish if you have large | ||
| 865 | database. | ||
| 866 | |||
| 867 | Example: | ||
| 868 | |||
| 869 | $customer->drop_placeholder('Orders'); | ||
| 870 | |||
| 871 | There is currently no way to rename a placeholder. You can create new | ||
| 872 | one, copy data from the old one to the new one and then drop old one. | ||
| 873 | |||
| 874 | If you drop a list placeholder it will effectively chop off entire | ||
| 875 | branch that starts at that placeholder and drop all related tables in | ||
| 876 | the SQL database. | ||
| 877 | |||
| 878 | =cut | ||
| 879 | |||
| 880 | sub drop_placeholder ($$) { | ||
| 881 | my $self=shift; | ||
| 882 | my $name=shift; | ||
| 883 | |||
| 884 | my $field=$self->_field_description($name) || | ||
| 885 | $self->throw("drop_placeholder - placeholder does not exist ($name)"); | ||
| 886 | |||
| 887 | if($field->{'type'} eq 'list') { | ||
| 888 | $self->_drop_list_placeholder($name); | ||
| 889 | } else { | ||
| 890 | $self->_drop_data_placeholder($name); | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | ############################################################################### | ||
| 895 | |||
| 896 | =item exists ($) | ||
| 897 | |||
| 898 | Checks if there is a placeholder for the given key. Here is an example: | ||
| 899 | |||
| 900 | if(! $customer->exists('middle_name')) { | ||
| 901 | print "No placeholder for 'middle_name' exists in the database\n"; | ||
| 902 | } | ||
| 903 | |||
| 904 | Not the same as defined() which just checks if given property has value | ||
| 905 | or not. Property can have placeholder and still be undefined. | ||
| 906 | |||
| 907 | =cut | ||
| 908 | |||
| 909 | sub exists ($$) { | ||
| 910 | my $self=shift; | ||
| 911 | my $name=shift; | ||
| 912 | return 1 if $name eq 'unique_id'; | ||
| 913 | my $f=$self->_class_description->{'fields'}; | ||
| 914 | exists $f->{$name}; | ||
| 915 | } | ||
| 916 | |||
| 917 | ############################################################################### | ||
| 918 | |||
| 919 | =item fetch ($) | ||
| 920 | |||
| 921 | Takes URI style path and returns an object or property referenced by | ||
| 922 | that path. If path starts with slash (/) then it goes from the top, | ||
| 923 | otherwise - from the current object. Counting objects from top would | ||
| 924 | always give you connected object or undef. | ||
| 925 | |||
| 926 | Examples: | ||
| 927 | |||
| 928 | my $zip001=$customer->fetch('Addresses/addr001/zipcode'); | ||
| 929 | |||
| 930 | my $customers=$address->fetch('/Customers'); | ||
| 931 | |||
| 932 | B<Currently is only implemented on Glue, relative URI are not | ||
| 933 | supported>. | ||
| 934 | |||
| 935 | =cut | ||
| 936 | |||
| 937 | ## TODO | ||
| 938 | |||
| 939 | ############################################################################### | ||
| 940 | |||
| 941 | =item fill (%) | ||
| 942 | |||
| 943 | Takes a hash or another object of the same type and merges all the data | ||
| 944 | from it into the current object. | ||
| 945 | |||
| 946 | B<NOT IMPLEMENTED>. | ||
| 947 | |||
| 948 | =cut | ||
| 949 | |||
| 950 | ## TODO | ||
| 951 | |||
| 952 | ############################################################################### | ||
| 953 | |||
| 954 | =item get ($) | ||
| 955 | |||
| 956 | Retrieves data field or a list reference from the object. | ||
| 957 | |||
| 958 | Example: | ||
| 959 | |||
| 960 | my $addresses_list=$customer->get('Addresses'); | ||
| 961 | |||
| 962 | my $first_name=$customer->get('first_name'); | ||
| 963 | |||
| 964 | As a convenience (and an optimisation, because database driver would be | ||
| 965 | able to optimise that into just one query into database in most cases) | ||
| 966 | you can pass more then one property name into the get() method. In that | ||
| 967 | case it will return you an array of values in the same order that you | ||
| 968 | passed property names. | ||
| 969 | |||
| 970 | my ($name,$phone,$fax)=$customer->get('name','phone','fax'); | ||
| 971 | |||
| 972 | =cut | ||
| 973 | |||
| 974 | sub get ($$) { | ||
| 975 | my $self=shift; | ||
| 976 | |||
| 977 | @_ || $self->throw("get - at least one property name must be passed"); | ||
| 978 | |||
| 979 | if(@_ == 1) { | ||
| 980 | |||
| 981 | my $name=shift; | ||
| 982 | |||
| 983 | my $field=$self->_field_description($name) || | ||
| 984 | $self->throw("get - unknown property ($name)"); | ||
| 985 | |||
| 986 | my $type=$field->{'type'}; | ||
| 987 | if($$self->{'detached'}) { | ||
| 988 | if($type eq 'list') { | ||
| 989 | $self->throw("get - retrieval of lists on detached objects not implemented yet"); | ||
| 990 | } else { | ||
| 991 | my $value=$$self->{'data'}->{$name}; | ||
| 992 | $value=$self->_field_default($name,$field) unless defined($value); | ||
| 993 | return $value; | ||
| 994 | } | ||
| 995 | } | ||
| 996 | else { | ||
| 997 | if($type eq 'list') { | ||
| 998 | return XAO::Objects->new( | ||
| 999 | objname => 'FS::List', | ||
| 1000 | glue => $self->_glue, | ||
| 1001 | uri => $self->uri($name), | ||
| 1002 | class_name => $field->{'class'}, | ||
| 1003 | base_name => $$self->{'objname'}, | ||
| 1004 | base_id => $$self->{'unique_id'}, | ||
| 1005 | key_value => $name); | ||
| 1006 | } | ||
| 1007 | else { | ||
| 1008 | my $value=$self->_retrieve_data_fields($name); | ||
| 1009 | |||
| 1010 | defined $value || | ||
| 1011 | throw $self "get('$name') - db query returned undef"; | ||
| 1012 | |||
| 1013 | if($type eq 'text') { | ||
| 1014 | my $charset=$field->{'charset'}; | ||
| 1015 | if($charset ne 'binary' && !Encode::is_utf8($value)) { | ||
| 1016 | $value=Encode::decode($charset,$value,Encode::FB_DEFAULT); | ||
| 1017 | } | ||
| 1018 | } | ||
| 1019 | elsif($type eq 'real' && !(0+$value)) { | ||
| 1020 | $value=0; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | return $value; | ||
| 1024 | } | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | ## | ||
| 1029 | # Multiple keywords | ||
| 1030 | # | ||
| 1031 | else { | ||
| 1032 | |||
| 1033 | my $fields=$self->_class_description->{'fields'} || | ||
| 1034 | $self->throw("- internal data problem"); | ||
| 1035 | |||
| 1036 | my @datanames=map { | ||
| 1037 | my $f=$fields->{$_} || $self->throw("get - unknown property ($_)"); | ||
| 1038 | $f->{'type'} eq 'list' ? () : ($_); | ||
| 1039 | } @_; | ||
| 1040 | |||
| 1041 | if($$self->{'detached'}) { | ||
| 1042 | scalar(@datanames) == scalar(@_) || | ||
| 1043 | $self->throw("- retrieval of lists on detached objects not implemented yet"); | ||
| 1044 | |||
| 1045 | return map { | ||
| 1046 | my $value=$$self->{'data'}->{$_}; | ||
| 1047 | if(!defined($value)) { | ||
| 1048 | $value=$self->_field_default($_,$fields->{$_}); | ||
| 1049 | } | ||
| 1050 | $value; | ||
| 1051 | } @_; | ||
| 1052 | } | ||
| 1053 | else { | ||
| 1054 | my %datahash; | ||
| 1055 | |||
| 1056 | if(@datanames) { | ||
| 1057 | @datahash{@datanames}=$self->_retrieve_data_fields(@datanames); | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | return map { | ||
| 1061 | my $type=$fields->{$_}->{'type'}; | ||
| 1062 | |||
| 1063 | if($type eq 'list') { | ||
| 1064 | XAO::Objects->new(objname => 'FS::List', | ||
| 1065 | glue => $self->_glue, | ||
| 1066 | uri => $self->uri($_), | ||
| 1067 | class_name => $fields->{$_}->{'class'}, | ||
| 1068 | base_name => $$self->{'objname'}, | ||
| 1069 | base_id => $$self->{'unique_id'}, | ||
| 1070 | key_value => $_); | ||
| 1071 | } | ||
| 1072 | else { | ||
| 1073 | my $value=$datahash{$_}; | ||
| 1074 | |||
| 1075 | defined $value || | ||
| 1076 | throw $self "get('$_') - db query returned undef"; | ||
| 1077 | |||
| 1078 | if($type eq 'text') { | ||
| 1079 | my $charset=$fields->{$_}->{'charset'}; | ||
| 1080 | if($charset ne 'binary' && !Encode::is_utf8($value)) { | ||
| 1081 | $value=Encode::decode($charset,$value,Encode::FB_DEFAULT); | ||
| 1082 | } | ||
| 1083 | } | ||
| 1084 | elsif($type eq 'real' && !(0+$value)) { | ||
| 1085 | $value=0; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | $value; | ||
| 1089 | } | ||
| 1090 | } @_; | ||
| 1091 | } | ||
| 1092 | } | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | ############################################################################### | ||
| 1096 | |||
| 1097 | =item glue () | ||
| 1098 | |||
| 1099 | Returns the Glue object which was used to retrieve the current object | ||
| 1100 | from. | ||
| 1101 | |||
| 1102 | =cut | ||
| 1103 | |||
| 1104 | # Implemented in Glue | ||
| 1105 | |||
| 1106 | ############################################################################### | ||
| 1107 | |||
| 1108 | =item is_attached () | ||
| 1109 | |||
| 1110 | Boolean method that returns true if the current object is attached or | ||
| 1111 | false otherwise. | ||
| 1112 | |||
| 1113 | =cut | ||
| 1114 | |||
| 1115 | sub is_attached ($) { | ||
| 1116 | my $self=shift; | ||
| 1117 | ! $$self->{'detached'}; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | ############################################################################### | ||
| 1121 | |||
| 1122 | =item keys () | ||
| 1123 | |||
| 1124 | Returns list of field names and list names stored in that | ||
| 1125 | object. Excludes connectors and unique_id. | ||
| 1126 | |||
| 1127 | Example: | ||
| 1128 | |||
| 1129 | foreach my $key ($object->keys) { | ||
| 1130 | my $value=$object->get($key); | ||
| 1131 | print "object.$key=$value\n"; | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | =cut | ||
| 1135 | |||
| 1136 | sub keys ($) { | ||
| 1137 | my $self=shift; | ||
| 1138 | |||
| 1139 | my $fields=$self->_class_description()->{'fields'}; | ||
| 1140 | my @list; | ||
| 1141 | foreach my $key (keys %{$fields}) { | ||
| 1142 | next if $fields->{$key}->{'type'} eq 'connector'; | ||
| 1143 | push(@list,$key); | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | @list; | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | ############################################################################### | ||
| 1150 | |||
| 1151 | =item new (%) | ||
| 1152 | |||
| 1153 | Creates new instance of a Hash object. Would not work if called | ||
| 1154 | directly, XAO::Objects' new() method should always be used instead. | ||
| 1155 | |||
| 1156 | Example: | ||
| 1157 | |||
| 1158 | my $obj=XAO::Objects->new(objname => 'Data::Customer', | ||
| 1159 | glue => $glue); | ||
| 1160 | |||
| 1161 | One required argument is 'glue', it must contain a reference to | ||
| 1162 | L<XAO::DO::FS::Glue> object. | ||
| 1163 | |||
| 1164 | As a convenience it is recommended to call get_new() method on the list | ||
| 1165 | object to get new empty detached objects of the class that list can | ||
| 1166 | store. If you do so first you will not forget to pass `glue' as | ||
| 1167 | get_new() will do that for you and second if later on the name of the | ||
| 1168 | class will change you will not have to worry about that. | ||
| 1169 | |||
| 1170 | Example: | ||
| 1171 | |||
| 1172 | my $customer=$customers_list->get_new(); | ||
| 1173 | |||
| 1174 | =cut | ||
| 1175 | |||
| 1176 | sub new ($%) { | ||
| 1177 | my $class=shift; | ||
| 1178 | my $self=$class->SUPER::new(@_); | ||
| 1179 | my $args=get_args(\@_); | ||
| 1180 | |||
| 1181 | $$self->{'unique_id'}=$args->{'unique_id'}; | ||
| 1182 | |||
| 1183 | if(! $$self->{'unique_id'}) { | ||
| 1184 | $$self->{'detached'}=1; | ||
| 1185 | } | ||
| 1186 | else { | ||
| 1187 | $$self->{'key_name'}=$args->{'key_name'}; | ||
| 1188 | defined($$self->{'key_name'}) || $self->throw("new - no 'key_name' passed"); | ||
| 1189 | $$self->{'key_value'}=$args->{'key_value'} || | ||
| 1190 | $self->get($args->{'key_name'}); | ||
| 1191 | |||
| 1192 | $$self->{'list_base_name'}=$args->{'list_base_name'}; | ||
| 1193 | $$self->{'list_base_id'}=$args->{'list_base_id'}; | ||
| 1194 | $$self->{'list_key_value'}=$args->{'list_key_value'}; | ||
| 1195 | |||
| 1196 | if(! defined($$self->{'uri'})) { | ||
| 1197 | my $uri=$$self->{'key_value'}; | ||
| 1198 | my $p=$self; | ||
| 1199 | while(defined($p=$p->container_object)) { | ||
| 1200 | my $ck=$p->container_key(); | ||
| 1201 | $ck='' unless defined($ck); | ||
| 1202 | $uri=$ck . '/' . $uri; | ||
| 1203 | } | ||
| 1204 | $$self->{'uri'}=$uri; | ||
| 1205 | } | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | $self; | ||
| 1209 | } | ||
| 1210 | |||
| 1211 | ############################################################################### | ||
| 1212 | |||
| 1213 | =item objname () | ||
| 1214 | |||
| 1215 | Returns class name of the object. | ||
| 1216 | |||
| 1217 | =cut | ||
| 1218 | |||
| 1219 | # implemented in Glue.pm | ||
| 1220 | |||
| 1221 | ############################################################################### | ||
| 1222 | |||
| 1223 | =item objtype () | ||
| 1224 | |||
| 1225 | For all objects based on Hash returns 'Hash' string. | ||
| 1226 | |||
| 1227 | =cut | ||
| 1228 | |||
| 1229 | sub objtype ($) { | ||
| 1230 | 'Hash'; | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | ############################################################################### | ||
| 1234 | |||
| 1235 | =item put (%) | ||
| 1236 | |||
| 1237 | Stores new value into the Hash object. Values can currently only be | ||
| 1238 | strings and numbers, you cannot store a list. | ||
| 1239 | |||
| 1240 | On attached objects your changes go directly into the database. On | ||
| 1241 | detached objects changes accumulate in memory and only get stored into | ||
| 1242 | the database when you put() that object into an attached list. | ||
| 1243 | |||
| 1244 | Example: | ||
| 1245 | |||
| 1246 | $customer->put(full_name => 'John Silver'); | ||
| 1247 | |||
| 1248 | Name must correspond to a previously defined placeholder otherwise an | ||
| 1249 | error will be thrown. | ||
| 1250 | |||
| 1251 | Value must meet constrains set for the placeholder, otherwise error will | ||
| 1252 | be thrown and no changes will be made. | ||
| 1253 | |||
| 1254 | More then one name/value pair can be given on the same line. For | ||
| 1255 | example, all three code snippets below will have the same effect, but | ||
| 1256 | first will be slower: | ||
| 1257 | |||
| 1258 | # One by one, the slowest way. | ||
| 1259 | # | ||
| 1260 | $obj->put(first_name => 'John'); | ||
| 1261 | $obj->put(last_name => 'Silver'); | ||
| 1262 | $obj->put(age => '50'); | ||
| 1263 | |||
| 1264 | # Hash-like array, will translate to one SQL update statement. | ||
| 1265 | # | ||
| 1266 | $obj->put(first_name => 'John', last_name => 'Silver', age => 50); | ||
| 1267 | |||
| 1268 | # Hash reference | ||
| 1269 | # | ||
| 1270 | my %data=( | ||
| 1271 | first_name => 'John', | ||
| 1272 | last_name => 'Silver', | ||
| 1273 | age => 50, | ||
| 1274 | ); | ||
| 1275 | $obj->put(\%data); | ||
| 1276 | |||
| 1277 | =cut | ||
| 1278 | |||
| 1279 | sub put ($$$) { | ||
| 1280 | my $self=shift; | ||
| 1281 | my $data=get_args(\@_); | ||
| 1282 | |||
| 1283 | my $detached=$$self->{'detached'}; | ||
| 1284 | |||
| 1285 | my @data_keys=CORE::keys %$data; | ||
| 1286 | foreach my $name (@data_keys) { | ||
| 1287 | my $value=$data->{$name}; | ||
| 1288 | |||
| 1289 | my $field=$self->_field_description($name); | ||
| 1290 | $field || $self->throw("- {{INTERNAL:Not defined field name '$name'}}"); | ||
| 1291 | |||
| 1292 | if(!defined $value) { | ||
| 1293 | $data->{$name}=$value=$self->_field_default($name,$field); | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | my $type=$field->{'type'}; | ||
| 1297 | if($type eq 'list') { | ||
| 1298 | $self->throw("- {{INTERNAL:Storing lists not implemented yet}}"); | ||
| 1299 | } | ||
| 1300 | elsif($type eq 'key') { | ||
| 1301 | $self->throw("- {{INTERNAL:Attempt to modify hash key}}"); | ||
| 1302 | } | ||
| 1303 | elsif($name eq 'unique_id') { | ||
| 1304 | $self->throw("- {{INTERNAL:Attempt to modify unique_id}}"); | ||
| 1305 | } | ||
| 1306 | elsif($type eq 'blob') { | ||
| 1307 | if(Encode::is_utf8($value)) { | ||
| 1308 | $data->{$name}=$value=Encode::encode('utf8',$value); | ||
| 1309 | } | ||
| 1310 | length($value) <= $field->{'maxlength'} || | ||
| 1311 | $self->throw("- {{INPUT:Value is longer than $field->{'maxlength'} for '$name'}}"); | ||
| 1312 | } | ||
| 1313 | elsif($type eq 'text') { | ||
| 1314 | if(Encode::is_utf8($value)) { | ||
| 1315 | length($value) <= $field->{'maxlength'} || | ||
| 1316 | $self->throw("- {{INPUT:Value is longer than $field->{'maxlength'} for '$name'}}"); | ||
| 1317 | |||
| 1318 | my $charset=$field->{'charset'}; | ||
| 1319 | |||
| 1320 | if($value=~/[\x{10000}-\x{ffffffff}]/ && $charset eq 'utf8') { | ||
| 1321 | $self->throw("- {{INPUT:Unsupported supplemental unicode value for '$name'}}"); | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | $data->{$name}=$value=Encode::encode($charset eq 'binary' ? 'utf8' : $charset,$value); | ||
| 1325 | } | ||
| 1326 | if($field->{'charset'} eq 'binary') { | ||
| 1327 | length($value) <= $field->{'maxlength'} || | ||
| 1328 | $self->throw("- {{INPUT:Value is longer than $field->{'maxlength'} for '$name'}}"); | ||
| 1329 | } | ||
| 1330 | else { | ||
| 1331 | # Checking characters length with binary source is | ||
| 1332 | # too expensive, not doing it This should be a rare | ||
| 1333 | # situation anyway, and MySQL will chop off the extra if | ||
| 1334 | # needed. | ||
| 1335 | } | ||
| 1336 | } | ||
| 1337 | elsif($type eq 'integer' || $type eq 'real') { | ||
| 1338 | $data->{$name}=$value=$self->_field_default($name,$field) if $value eq ''; | ||
| 1339 | |||
| 1340 | if($type eq 'integer') { | ||
| 1341 | $data->{$name}=$value=int($value); | ||
| 1342 | } | ||
| 1343 | elsif(defined $field->{'scale'}) { | ||
| 1344 | $data->{$name}=$value=($value!=0 ? sprintf('%0.*f',$field->{'scale'},$value) : 0); | ||
| 1345 | } | ||
| 1346 | else { | ||
| 1347 | $data->{$name}=($value+=0); | ||
| 1348 | } | ||
| 1349 | |||
| 1350 | !defined($field->{'minvalue'}) || $value>=$field->{'minvalue'} || | ||
| 1351 | $self->throw("- {{INPUT:Value ($value) is less then $field->{'minvalue'} for $name}}"); | ||
| 1352 | |||
| 1353 | !defined($field->{'maxvalue'}) || $value<=$field->{'maxvalue'} || | ||
| 1354 | $self->throw("- {{INPUT:Value ($value) is bigger then $field->{'maxvalue'} for $name}}"); | ||
| 1355 | } | ||
| 1356 | else { | ||
| 1357 | throw $self "- {{INTERNAL:Unknown field type '$type'}}"; | ||
| 1358 | } | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | # Storing all values at once | ||
| 1362 | # | ||
| 1363 | if($detached) { | ||
| 1364 | @{$$self->{'data'}}{@data_keys}=values %$data; | ||
| 1365 | } | ||
| 1366 | else { | ||
| 1367 | $self->_store_data_fields($data); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | # For multi-value put with hash reference it will return undef, | ||
| 1371 | # but that's OK -- there is no good answer to what "the value" is | ||
| 1372 | # anyway. | ||
| 1373 | # | ||
| 1374 | return $_[1]; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | ############################################################################### | ||
| 1378 | |||
| 1379 | =item reattach () | ||
| 1380 | |||
| 1381 | The same as to call put() method on upper level container. Works only | ||
| 1382 | for detached objects and does nothing if the object is already attached. | ||
| 1383 | |||
| 1384 | B<Note:> entire content of an object currently stored in the database | ||
| 1385 | would be replaced with current object recursively. Be careful! | ||
| 1386 | |||
| 1387 | All attached objects that refer to the same piece of data would | ||
| 1388 | immediately start returning updated values on calls to get() method. | ||
| 1389 | |||
| 1390 | B<Not implemented yet. Call put() on the list instead to attach the object>. | ||
| 1391 | |||
| 1392 | =cut | ||
| 1393 | |||
| 1394 | ## TODO | ||
| 1395 | |||
| 1396 | ############################################################################### | ||
| 1397 | |||
| 1398 | =item upper_class (;$) | ||
| 1399 | |||
| 1400 | Returns class name for the hash that contained the list that contained | ||
| 1401 | current hash object. | ||
| 1402 | |||
| 1403 | =cut | ||
| 1404 | |||
| 1405 | sub upper_class ($;$) { | ||
| 1406 | my $self=shift; | ||
| 1407 | my $class_name=shift || $self->objname; | ||
| 1408 | $self->_glue->upper_class($class_name); | ||
| 1409 | } | ||
| 1410 | |||
| 1411 | ############################################################################### | ||
| 1412 | |||
| 1413 | =item values () | ||
| 1414 | |||
| 1415 | Returns a list of all property values for the current object including | ||
| 1416 | its contained Lists if any. | ||
| 1417 | |||
| 1418 | B<Note:> the order of values is the same as the order of keys returned | ||
| 1419 | by keys() method. At least until you modify the object directly on | ||
| 1420 | indirectly. It is not recommended to use values() method for the reason | ||
| 1421 | of pure predictability. | ||
| 1422 | |||
| 1423 | =cut | ||
| 1424 | |||
| 1425 | # implemented in Glue.pm | ||
| 1426 | |||
| 1427 | ############################################################################### | ||
| 1428 | |||
| 1429 | =item uri ($) | ||
| 1430 | |||
| 1431 | Returns complete URI to either the object itself (if no argument is | ||
| 1432 | given) or to a property with the given name. | ||
| 1433 | |||
| 1434 | That URI can then be used to retrieve a property or object using | ||
| 1435 | $odb->fetch($uri). Be aware, that fetch() is a relatively slow method | ||
| 1436 | and should not be abused. | ||
| 1437 | |||
| 1438 | Example: | ||
| 1439 | |||
| 1440 | my $uri=$customer->uri; | ||
| 1441 | print "URI of that customer is: $uri\n"; | ||
| 1442 | |||
| 1443 | =cut | ||
| 1444 | |||
| 1445 | # Implemented in Glue | ||
| 1446 | |||
| 1447 | ############################################################################### | ||
| 1448 | 1; | ||
| 1449 | __END__ |