| 1 | use strict; | ||
| 2 | |||
| 3 | package Tamino::Tran; | ||
| 4 | use base qw/Class::Accessor Class::Data::Inheritable/; | ||
| 5 | |||
| 6 | use XML::Bare 0.271; | ||
| 7 | |||
| 8 | use Tamino::Tran::Prepared; | ||
| 9 | use Tamino::Tran::Cursor; | ||
| 10 | |||
| 11 | # require MIME::Base64 follows | ||
| 12 | # require XML::Twig follows | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Tamino::Tran - The L<Tamino> driver's main class wrapping Tamino API. | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Tamino; | ||
| 21 | |||
| 22 | my $tamino_client = Tamino->new( | ||
| 23 | server => '127.0.0.1/tamino' | ||
| 24 | db => 'mydb' | ||
| 25 | ); | ||
| 26 | |||
| 27 | # $t will be a Tamino::Tran object | ||
| 28 | my $t = $tamino_client->begin_tran | ||
| 29 | or die $tamino_client->error; | ||
| 30 | |||
| 31 | $c = $t->xquery(q{for $x in input()/ return $x}) or die $t->error; | ||
| 32 | |||
| 33 | $c = $t->xquery_cursor(q{ | ||
| 34 | for $x in collection('mycollection')/doctype/xxx[@yyy=%s][zzz='%s'] | ||
| 35 | return $x | ||
| 36 | }, "('y1','y2')", "z1") or die $t->error; | ||
| 37 | |||
| 38 | while($xml_bare_simple_tree = $c->fetch) { | ||
| 39 | print XML::Simple::XMLout($xml_bare_simple_tree, KeyAttr => []); | ||
| 40 | } | ||
| 41 | |||
| 42 | $t->delete(q{for $x in input()/doc[@bad='yes'] return $x}) or die $t->error; | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | This is just an API wrapper. | ||
| 47 | This driver is based on L<LWP::UserAgent>, L<XML::Bare>, and inherits from L<Class::Accessor> and L<Class::Data::Inheritable>. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | |||
| 52 | __PACKAGE__->mk_ro_accessors(qw/url ua tamino _isolation_level _lock_mode _lock_wait _encoding messages/); | ||
| 53 | __PACKAGE__->mk_accessors(qw/_sid _sk isolation_level lock_mode lock_wait _accept_session encoding queries queries_time/); | ||
| 54 | |||
| 55 | __PACKAGE__->mk_classdata($_) for qw/prepared_class cursor_class xml_twig_class xml_twig_elt_class _debug pretty_print /; | ||
| 56 | __PACKAGE__->mk_accessors(qw/error prepared_class cursor_class xml_twig_class xml_twig_elt_class _debug pretty_print /); | ||
| 57 | |||
| 58 | __PACKAGE__->prepared_class('Tamino::Tran::Prepared'); | ||
| 59 | __PACKAGE__->cursor_class('Tamino::Tran::Cursor'); | ||
| 60 | __PACKAGE__->xml_twig_class('XML::Twig'); | ||
| 61 | __PACKAGE__->xml_twig_elt_class('XML::Twig::Elt'); | ||
| 62 | |||
| 63 | __PACKAGE__->pretty_print('indented'); | ||
| 64 | |||
| 65 | my $isolation_levels = { | ||
| 66 | map {$_=>$_} qw /uncommittedDocument committedCommand stableCursor stableDocument stableDocument/ | ||
| 67 | }; | ||
| 68 | |||
| 69 | my $lock_modes = { | ||
| 70 | map {$_=>$_} qw/unprotected shared protected/ | ||
| 71 | }; | ||
| 72 | |||
| 73 | my $yesno = { | ||
| 74 | map {$_=>$_} qw/yes no/ | ||
| 75 | }; | ||
| 76 | |||
| 77 | sub parse_xml { | ||
| 78 | my $self = shift; | ||
| 79 | my $xml = XML::Bare->new(text => ${$_[0]}, forcearray => { | ||
| 80 | map {$_ => 1} (@{$self->{_forcearray}}, @_, qw/ino:message ino:messagetext ino:messageline/) | ||
| 81 | }); | ||
| 82 | return unless ($xml->simple()->{'ino:response'}); | ||
| 83 | return $xml->{xml}->{'ino:response'}; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 CONSTRUCTOR | ||
| 87 | |||
| 88 | Constructor is called internally by L<Tamino> class object. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub new ($) { | ||
| 93 | my $class = shift; | ||
| 94 | my %args = @_; | ||
| 95 | $class = ref $class || $class; | ||
| 96 | my $self = $class->SUPER::new({ | ||
| 97 | _isolation_level => $isolation_levels->{$args{isolation_level}} || 'committedCommand', | ||
| 98 | _lock_mode => $lock_modes->{$args{lock_mode}}, | ||
| 99 | _lock_wait => $yesno->{$args{lock_wait}} || 'yes', | ||
| 100 | encoding => $args{encoding} || 'UTF-8', | ||
| 101 | |||
| 102 | pretty_print => $class->pretty_print, | ||
| 103 | xml_twig_class => $class->xml_twig_class, | ||
| 104 | xml_twig_elt_class=>$class->xml_twig_elt_class, | ||
| 105 | |||
| 106 | cursor_class => $class->cursor_class, | ||
| 107 | prepared_class => $class->prepared_class, | ||
| 108 | |||
| 109 | queries => 0, | ||
| 110 | queries_time => 0, | ||
| 111 | |||
| 112 | (map { $_ => $args{$_} } grep {exists $args{$_}} qw/url ua tamino _debug/), | ||
| 113 | }); | ||
| 114 | $self->_accept_session(0); | ||
| 115 | |||
| 116 | unless($args{_no_connect}) { | ||
| 117 | unless($self->connect) { | ||
| 118 | $class->error($self->error); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub dbg { | ||
| 127 | my $obj=shift; | ||
| 128 | my $w = (ref$obj||$obj)."::".sprintf(shift, @_)."::\n"; | ||
| 129 | warn $w; | ||
| 130 | } | ||
| 131 | |||
| 132 | my $__t; | ||
| 133 | sub _cmd ($$;@) { | ||
| 134 | my ($self, $data, %opts) = @_; | ||
| 135 | my @h; | ||
| 136 | |||
| 137 | $self->{queries}++; | ||
| 138 | $self->{tamino}->{queries}++; | ||
| 139 | |||
| 140 | $self->error(''); | ||
| 141 | $self->{messages} = ''; | ||
| 142 | if($self->_sid && $self->_sk) { | ||
| 143 | push @h, "X-INO-Sessionid" => $self->_sid, | ||
| 144 | "X-INO-Sessionkey" => $self->_sk; | ||
| 145 | } | ||
| 146 | |||
| 147 | $data->{_isolationLevel} = $isolation_levels->{$self->{isolation_level}} if($self->{isolation_level}); | ||
| 148 | $data->{_lockMode} = $lock_modes->{$self->{lock_mode}} if($self->{lock_mode}); | ||
| 149 | $data->{_lockWait} = $yesno->{$self->{lock_wait}} if($self->{lock_wait}); | ||
| 150 | $data->{_encoding} = $self->{encoding} if($self->{encoding}); | ||
| 151 | |||
| 152 | if($opts{send_session}) { | ||
| 153 | $data->{_sessionid} = $self->{_sid}; | ||
| 154 | $data->{_sessionkey} = $self->{_sk}; | ||
| 155 | } | ||
| 156 | |||
| 157 | $self->dbg("send==".join("&", map { "$_=$data->{$_}" } keys %$data )) if($self->{_debug}); | ||
| 158 | |||
| 159 | $__t = timer->new( \($self->{queries_time}, $self->{tamino}->{queries_time}) ); | ||
| 160 | my $r = $self->ua->post($self->{url}, @h, Content => [%$data]); | ||
| 161 | $self->dbg("TIME=%.6f",$__t->close) if($self->{_debug}); | ||
| 162 | undef $__t; | ||
| 163 | |||
| 164 | if($r->is_success) { | ||
| 165 | $self->dbg("OK...") if($self->{_debug}); | ||
| 166 | if($self->{_accept_session}) { | ||
| 167 | unless($r->header('X-INO-Sessionid') && $r->header('X-INO-Sessionkey')) { | ||
| 168 | $self->error('Session was not established'); | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | $self->{_sid} = $r->header('X-INO-Sessionid'); | ||
| 172 | $self->{_sk} = $r->header('X-INO-Sessionkey'); | ||
| 173 | $self->dbg("Session established: %s %s", $self->{_sid}, $self->{_sk}) if($self->{_debug}); | ||
| 174 | } elsif($self->{_sid}) { | ||
| 175 | if($self->{_sid} != $r->header('X-INO-Sessionid')) { | ||
| 176 | $self->error('Session broken'); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | $self->{_sk} = $r->header('X-INO-Sessionkey'); | ||
| 180 | $self->dbg('Session continued: %s %s', $self->{_sid}, $self->{_sk}) if($self->{_debug}); | ||
| 181 | } | ||
| 182 | return $self->_parse_response($r, %opts); | ||
| 183 | } else { | ||
| 184 | $self->error("HTTP Error %d: %s", $r->code, $r->message); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | sub _parse_response ($$@) { | ||
| 191 | my ($self, $r, %opts) = @_; | ||
| 192 | |||
| 193 | my $d = $r->content; | ||
| 194 | $self->dbg("RESULT: ===============\n%s\n===============",$d) if($self->{_debug}); | ||
| 195 | |||
| 196 | my $x = $self->parse_xml(\$d, | ||
| 197 | (wantarray ? ( $opts{result} ) : ()), | ||
| 198 | ($opts{_twig_handlers} ? (map { m{\w+$} && $& } keys %{$opts{_twig_handlers}}) : ()) | ||
| 199 | ) or $self->error("Bad XML received!") and return; | ||
| 200 | |||
| 201 | my $ret = 0; | ||
| 202 | for my $m (@{$x->{'ino:message'}}) { | ||
| 203 | my $retval = $m->{'ino:returnvalue'}; | ||
| 204 | if($retval && !$ret) { | ||
| 205 | $ret = $retval; | ||
| 206 | $self->error($ret.": ".join('\n', map { $_->{'ino:code'}.": ".$_->{value} } @{$m->{'ino:messagetext'}}) ); | ||
| 207 | } | ||
| 208 | $self->{messages} .= $ret.": ".join('\n', @{$m->{'ino:messageline'}})."\n"; | ||
| 209 | } | ||
| 210 | return if ($ret); | ||
| 211 | |||
| 212 | if ( $opts{_twig_handlers} ) { | ||
| 213 | for my $h (keys %{$opts{_twig_handlers}}) { | ||
| 214 | my $n = $x; | ||
| 215 | $n = $n->{$_} or last for split'/',$h; | ||
| 216 | last unless $n; | ||
| 217 | $opts{_twig_handlers}->{$h}->(undef, $n); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | if($self->{_plaintext}) { | ||
| 222 | return \$d; | ||
| 223 | } | ||
| 224 | |||
| 225 | return $opts{result} ? ( | ||
| 226 | wantarray ? @{$x->{$opts{result}}} : ( | ||
| 227 | defined wantarray ? $x->{$opts{result}} : undef | ||
| 228 | )) : 1; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub _open_cursor ($$;$@) { | ||
| 232 | my ($self, $data, %opts) = @_; | ||
| 233 | return $self->cursor_class->new(tran => $self, data => $data, %opts); | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | =head1 METHODS | ||
| 238 | |||
| 239 | =head2 connect | ||
| 240 | |||
| 241 | =over 4 | ||
| 242 | |||
| 243 | $t->connect or die $t->error; | ||
| 244 | |||
| 245 | Starts new transaction session. Transaction is started implicitly by the first DB update action. | ||
| 246 | After this call, all operations are made in transaction context. | ||
| 247 | |||
| 248 | =back | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | sub connect ($) { | ||
| 253 | my ($self) = @_; | ||
| 254 | $self->{_accept_session} = 1; | ||
| 255 | my $ret = $self->_cmd({ | ||
| 256 | _connect => '*', | ||
| 257 | $self->{_isolation_level} ? ( _isolationLevel => $self->{_isolation_level} ) : (), | ||
| 258 | $self->{_lock_mode} ? ( _lockMode => $self->{_lock_mode} ) : (), | ||
| 259 | $self->{_lock_wait} ? ( _lockWait => $self->{_lock_wait} ) : (), | ||
| 260 | }); | ||
| 261 | $self->{_accept_session} = 0; | ||
| 262 | return defined $ret; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | =head2 disconnect | ||
| 267 | |||
| 268 | =over 4 | ||
| 269 | |||
| 270 | $t->disconnect or die $t->error; | ||
| 271 | |||
| 272 | Ends transaction session. All uncommitted data is rolled back. | ||
| 273 | After this call, all operations are made in non-transactional context. | ||
| 274 | |||
| 275 | =back | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | sub disconnect ($) { | ||
| 280 | my ($self) = @_; | ||
| 281 | if(defined $self->_cmd({ _disconnect => '*' }) ) { | ||
| 282 | $self->{_sid} = undef; | ||
| 283 | $self->{_sk} = undef; | ||
| 284 | return 1; | ||
| 285 | } | ||
| 286 | return 0; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub DESTROY { | ||
| 290 | my ($self) = @_; | ||
| 291 | $self->rollback; | ||
| 292 | $self->disconnect; | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | =head2 commit | ||
| 297 | |||
| 298 | =over 4 | ||
| 299 | |||
| 300 | $t->commit or die $t->error; | ||
| 301 | |||
| 302 | Commit changes. If you want such thing as autocommit - just don't start transaction session (C<< $t = $tamino_client->begin(); >>) | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub commit ($) { | ||
| 309 | my ($self) = @_; | ||
| 310 | return defined $self->_cmd({ _commit => '*' }) if($self->{_sk}); | ||
| 311 | return 1; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | =head2 rollback | ||
| 316 | |||
| 317 | =over 4 | ||
| 318 | |||
| 319 | $t->rollback or die $t->error; | ||
| 320 | |||
| 321 | Rollback changes. | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | sub rollback ($) { | ||
| 328 | my ($self) = @_; | ||
| 329 | return defined $self->_cmd({ _rollback => '*' }) if($self->{_sk}); | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | =head2 prepare | ||
| 335 | |||
| 336 | =over 4 | ||
| 337 | |||
| 338 | my $stmt = $t->prepare($query, \%vars) or die $t->error; | ||
| 339 | my $stmt = $t->prepare(q{for $x in input()/xxx[@yyy=$y][zzz=$z]}, { | ||
| 340 | y => 'string', | ||
| 341 | z => 'xs:integer' | ||
| 342 | }) or die $t->error; | ||
| 343 | |||
| 344 | Initializes a prepared statement. The C<$query> is compiled by server, and | ||
| 345 | can be executed later with parameters. Available only with Tamino v4.4+ | ||
| 346 | |||
| 347 | The C<\%vars> paramter specifies parameter types. Paramter names specified without B<$> sign. | ||
| 348 | |||
| 349 | Returns L<Tamino::Tran::Prepared> object. | ||
| 350 | |||
| 351 | =back | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | sub prepare ($$$) { | ||
| 356 | my ($self, $query, $vars) = @_; | ||
| 357 | return $self->prepared_class->new(tran => $self, query => $query, vars => $vars); | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | =head2 xquery | ||
| 362 | |||
| 363 | =over 4 | ||
| 364 | |||
| 365 | my $xml = $t->xquery($query_fmt, @args) or die $t->error; | ||
| 366 | my $xml = $t->xquery(q{ | ||
| 367 | for $x in collection('mycollection')/doctype/xxx[@yyy=%s][zzz='%s'] | ||
| 368 | return $x | ||
| 369 | }, "('y1','y2')", "z1") or die $t->error; | ||
| 370 | print XML::Simple::XMLout($xml); | ||
| 371 | |||
| 372 | Returns L<XML::Simple>-like tree object representing the result of C<< sprintf($query_fmt, @args) >>-X-Query | ||
| 373 | This L<sprintf|perlfunc/sprintf> trick is used to avoid interpolation crap, | ||
| 374 | because X-Query uses the same C<$var>-form variables, just like we do. | ||
| 375 | Look at L<plaintext|/plaintext> method if you want to get plain-text XMLs. | ||
| 376 | |||
| 377 | =back | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | sub xquery ($$;@) { | ||
| 382 | my ($self, $query) = (shift, shift); | ||
| 383 | return $self->_cmd({ _xquery => sprintf($query,@_) }, result => 'xq:result'); | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | =head2 xquery_cursor | ||
| 388 | |||
| 389 | =over 4 | ||
| 390 | |||
| 391 | my $cursor = $t->xquery_cursor($query_fmt, [\%cursor_opts,] @args) or die $t->error; | ||
| 392 | |||
| 393 | The same as L</xquery>, except that it opens cursor for the X-Query and | ||
| 394 | returns L<Tamino::Tran::Cursor> object. | ||
| 395 | |||
| 396 | Pass a HASHREF as 2-nd parameter to specify cursor options, otherwise it will be treated as the first of B<args> | ||
| 397 | I<cursor_options> can be: | ||
| 398 | |||
| 399 | C<< scrollable => 1 >> | ||
| 400 | |||
| 401 | C<< vague => 1 >> | ||
| 402 | |||
| 403 | C<< fetch_size => 1 >> | ||
| 404 | |||
| 405 | C<< no_fetch => 1 >> this tells Tamino server not to fetch-on-open. | ||
| 406 | |||
| 407 | For What-This-All-Means read Tamino Documentation. | ||
| 408 | |||
| 409 | =back | ||
| 410 | |||
| 411 | =cut | ||
| 412 | |||
| 413 | sub xquery_cursor ($$;@) { | ||
| 414 | my ($self, $query, $cursor_opts) = (shift, shift, shift); | ||
| 415 | if(ref $cursor_opts eq 'HASH') { | ||
| 416 | $cursor_opts = { map { $_ => $cursor_opts->{$_} } qw/scrollable vague fetch_size no_fetch/ }; | ||
| 417 | } else { | ||
| 418 | unshift @_, $cursor_opts; | ||
| 419 | } | ||
| 420 | return $self->_open_cursor({ _xquery => sprintf($query,@_) }, | ||
| 421 | result => 'xq:result', | ||
| 422 | %$cursor_opts | ||
| 423 | ); | ||
| 424 | } | ||
| 425 | |||
| 426 | |||
| 427 | =head2 xql | ||
| 428 | |||
| 429 | =over 4 | ||
| 430 | |||
| 431 | my $xml = $t->xql($query_fmt, @args) or die $t->error; | ||
| 432 | print XML::Simple::XMLout($xml); | ||
| 433 | |||
| 434 | The same as L</xquery>, except that it uses B<XQuery>, not B<X-Query>. | ||
| 435 | What is the difference? I don't know. Read the documentation for Tamino. | ||
| 436 | |||
| 437 | =back | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub xql ($$;@) { | ||
| 442 | my ($self, $query) = (shift, shift); | ||
| 443 | return $self->_cmd({ _xql => sprintf($query,@_) }, result => 'xql:result'); | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | =head2 xql_cursor | ||
| 448 | |||
| 449 | =over 4 | ||
| 450 | |||
| 451 | my $cursor = $t->xql_cursor($query_fmt, \%cursor_opts, @args) or die $t->error; | ||
| 452 | |||
| 453 | The same as L</xquery_cursor>, except that it uses B<XQuery>, not B<X-Query>. | ||
| 454 | What is the difference? I don't know. Read the documentation for Tamino. | ||
| 455 | |||
| 456 | =back | ||
| 457 | |||
| 458 | =cut | ||
| 459 | |||
| 460 | sub xql_cursor ($$;@) { | ||
| 461 | my ($self, $query, $cursor_opts) = (shift, shift, shift); | ||
| 462 | if(ref $cursor_opts eq 'HASH') { | ||
| 463 | $cursor_opts = { map { $_ => $cursor_opts->{$_} } qw/scrollable vague fetch_size no_fetch/ }; | ||
| 464 | } else { | ||
| 465 | unshift @_, $cursor_opts; | ||
| 466 | } | ||
| 467 | return $self->_open_cursor({ _xql => sprintf($query,@_) }, | ||
| 468 | result => 'xql:result', | ||
| 469 | %$cursor_opts | ||
| 470 | ); | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | =head2 delete | ||
| 475 | |||
| 476 | =over 4 | ||
| 477 | |||
| 478 | $t->delete($xquery_fmt, @args) or die $t->error; | ||
| 479 | |||
| 480 | Delete documents matching the X-Query. | ||
| 481 | Parameters are the same as for L</xquery>. | ||
| 482 | |||
| 483 | =back | ||
| 484 | |||
| 485 | =cut | ||
| 486 | |||
| 487 | sub delete ($$;@) { | ||
| 488 | my ($self, $query) = (shift, shift); | ||
| 489 | return defined $self->_cmd({ _delete => sprintf($query,@_) }); | ||
| 490 | } | ||
| 491 | |||
| 492 | =head2 process | ||
| 493 | |||
| 494 | =over 4 | ||
| 495 | |||
| 496 | $t->process( [ { name => $name, id => $id, data => \$xml, %options } , ... ], %OPTIONS ); | ||
| 497 | |||
| 498 | Takes ARRAYREF of documents and | ||
| 499 | submit a PROCESS command, which does the following for each document: | ||
| 500 | |||
| 501 | Replaces document if B<name> and/or B<id> specified (the document MUST exists, and B<name> MUST match B<id>). | ||
| 502 | Returns I<TRUE> on success. | ||
| 503 | |||
| 504 | Stores new document if neither B<name> nor B<id> was specified. | ||
| 505 | Returns ARRAYREF of HASHREFs of I<id>, I<name> and I<collection>. | ||
| 506 | |||
| 507 | B<data> parameter is a scalarref poiting to the [XML] document or an L<XML::Twig::Elt> object. | ||
| 508 | |||
| 509 | B<%options> may include: | ||
| 510 | |||
| 511 | C<< escape => 1 >> to specify that B<data> is an not XML string, so it will be escaped. | ||
| 512 | |||
| 513 | C<< base64 => 1 >> to Base64-encode B<data> string. | ||
| 514 | |||
| 515 | C<< collection => $my_collection_name >> to specify where to store documents. | ||
| 516 | You MUST provide this attribute if you haven't pass it into L<Tamino> constructor, | ||
| 517 | otherwise the default "ino:etc" collection will be used. | ||
| 518 | |||
| 519 | B<%OPTIONS> may include: | ||
| 520 | |||
| 521 | C<< encoding => $enc >> to specify encoding of DOCUMENTS being processed. | ||
| 522 | |||
| 523 | =back | ||
| 524 | |||
| 525 | =cut | ||
| 526 | |||
| 527 | sub process ($$;@) { | ||
| 528 | my ($self, $docs, %opts) = @_; | ||
| 529 | my $x = $self->xml_twig_class; | ||
| 530 | $x =~ s!::!/!gs; | ||
| 531 | require $x.".pm"; | ||
| 532 | my $xml = $self->xml_twig_class->new; | ||
| 533 | $xml->parse(sprintf( | ||
| 534 | q{<?xml version="1.0" encoding="%s"?> | ||
| 535 | <ino:request xmlns:ino="http://namespaces.softwareag.com/tamino/response2" />}, | ||
| 536 | $opts{encoding} || $self->encoding || $self->_encoding)); | ||
| 537 | |||
| 538 | # <ino:object ino:docname="name" ino:id="id" > | ||
| 539 | # data | ||
| 540 | # </ino:object> | ||
| 541 | |||
| 542 | my $i = 0; | ||
| 543 | my $inserting = 0; | ||
| 544 | my $root = $xml->root; | ||
| 545 | for my $d (@$docs) { | ||
| 546 | my $e = $self->xml_twig_elt_class->new('ino:object'); | ||
| 547 | $e->set_att('ino:id' => $d->{id}) if defined $d->{id}; | ||
| 548 | $e->set_att('ino:docname' => $d->{name}) if defined $d->{name}; | ||
| 549 | $e->set_att('ino:collection' => $d->{collection}) if defined $d->{collection}; | ||
| 550 | $e->paste(last_child => $root); | ||
| 551 | $inserting += int(!defined $d->{id} && !defined $d->{name}); | ||
| 552 | if(eval{!!$d->{data}->isa($self->xml_twig_class)}){ | ||
| 553 | $d->{data} = $d->{data}->root; | ||
| 554 | } | ||
| 555 | if(eval{!!$d->{data}->isa($self->xml_twig_elt_class)}) { | ||
| 556 | $d->{data}->copy->paste(last_child => $e); | ||
| 557 | } else { | ||
| 558 | if($d->{base64}) { | ||
| 559 | require MIME::Base64; | ||
| 560 | $e->set_text(MIME::Base64::encode(${$d->{data}})); | ||
| 561 | } elsif($d->{escape}) { | ||
| 562 | $e->set_text(${$d->{data}}); | ||
| 563 | } else { | ||
| 564 | my $xml = $self->xml_twig_class->new; | ||
| 565 | $xml->safe_parse(${$d->{data}}); | ||
| 566 | if($@) { | ||
| 567 | $self->error("Doc#%d: error in XML: %s", $i, $@) and return; | ||
| 568 | } | ||
| 569 | $xml->root->move(last_child => $e); | ||
| 570 | } | ||
| 571 | } | ||
| 572 | } continue { | ||
| 573 | $i++; | ||
| 574 | } | ||
| 575 | |||
| 576 | return defined $self->_cmd({ _process => $xml->sprint }) unless $inserting; | ||
| 577 | |||
| 578 | my @objs = $self->_cmd({ _process => $xml->sprint }, result => 'ino:object'); | ||
| 579 | return [ map +{ | ||
| 580 | id => $_->{'ino:id'}, | ||
| 581 | name => $_->{'ino:docname'}, | ||
| 582 | collection => $_->{'ino:collection'}, | ||
| 583 | }, @objs ]; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub define { | ||
| 587 | my ($self, $doc) = @_; | ||
| 588 | return defined $self->_cmd({ _define => $doc }); | ||
| 589 | } | ||
| 590 | |||
| 591 | sub define_collection ($$;$) { | ||
| 592 | my ($self, $name, $opt) = @_; | ||
| 593 | $opt ||= 'required'; | ||
| 594 | my $xml = qq{ | ||
| 595 | <tsd:collection name="$name" | ||
| 596 | xmlns:tsd="http://namespaces.softwareag.com/tamino/TaminoSchemaDefinition"> | ||
| 597 | <tsd:schema use="$opt"/> | ||
| 598 | </tsd:collection> | ||
| 599 | }; | ||
| 600 | return defined $self->_cmd({ _define => $xml }); | ||
| 601 | } | ||
| 602 | |||
| 603 | sub undefine { | ||
| 604 | my ($self, $doc) = @_; | ||
| 605 | return defined $self->_cmd({ _undefine => $doc }); | ||
| 606 | } | ||
| 607 | |||
| 608 | =head1 MISC METHODS | ||
| 609 | |||
| 610 | =over 4 | ||
| 611 | |||
| 612 | =item error | ||
| 613 | |||
| 614 | =item messages | ||
| 615 | |||
| 616 | print $t->messages; # any messages from server. | ||
| 617 | warn $t->error; | ||
| 618 | |||
| 619 | =back | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | sub error ($;@) { | ||
| 624 | my $self = shift; | ||
| 625 | if(@_) { | ||
| 626 | $self->{error} = sprintf(shift, @_); | ||
| 627 | $self->dbg("ERROR: %s",$self->{error}) if($self->{_debug}); | ||
| 628 | return 1; | ||
| 629 | } else { | ||
| 630 | return $self->{error}; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | |||
| 634 | sub simplify ($$) { | ||
| 635 | my ($self, $arg) = @_; | ||
| 636 | $self->{_simplify} = $arg; | ||
| 637 | return unless $arg; | ||
| 638 | my %a = @$arg; | ||
| 639 | $self->{_forcearray} = $a{forcearray}; | ||
| 640 | } | ||
| 641 | |||
| 642 | =pod | ||
| 643 | |||
| 644 | =over 4 | ||
| 645 | |||
| 646 | =item forcearray | ||
| 647 | |||
| 648 | $t->forcearray(qw/tag1 tag2/); | ||
| 649 | $t->forcearray([qw/tag1 tag2/]); | ||
| 650 | |||
| 651 | Force these tags to be represented as an array, even if there is only one. | ||
| 652 | |||
| 653 | =back | ||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | sub forcearray ($;@) { | ||
| 658 | my $self = shift; | ||
| 659 | $self->{_forcearray} = ref $_[0] ? $_[0] : [@_]; | ||
| 660 | } | ||
| 661 | |||
| 662 | =pod | ||
| 663 | |||
| 664 | =over 4 | ||
| 665 | |||
| 666 | =item plaintext | ||
| 667 | |||
| 668 | $t->plaintext($boolean); | ||
| 669 | |||
| 670 | If true, all requests that return an XML tree will return a SCALARREF to plain XML data | ||
| 671 | |||
| 672 | =back | ||
| 673 | |||
| 674 | =cut | ||
| 675 | |||
| 676 | sub plaintext ($;$) { | ||
| 677 | my $self = shift; | ||
| 678 | $self->{_plaintext} = $_[0]; | ||
| 679 | } | ||
| 680 | |||
| 681 | =pod | ||
| 682 | |||
| 683 | =over 4 | ||
| 684 | |||
| 685 | =item encoding | ||
| 686 | |||
| 687 | $t->encoding('other_encoding'); # change encoding | ||
| 688 | |||
| 689 | =back | ||
| 690 | |||
| 691 | =head2 TRANSACTION CONTROL METHODS | ||
| 692 | |||
| 693 | $t->isolation_level($level); | ||
| 694 | $t->lock_mode($mode); | ||
| 695 | $t->lock_wait($wait); | ||
| 696 | |||
| 697 | Set new transaction options. | ||
| 698 | The same as L<Tamino/begin_tran> options. | ||
| 699 | |||
| 700 | =head1 SUBCLASSING | ||
| 701 | |||
| 702 | You can subclass I<Tamino::Tran>. | ||
| 703 | You can tell I<Tamino::Tran> to use subclassed | ||
| 704 | L<XML::Twig>, | ||
| 705 | L<XML::Twig::Elt>, | ||
| 706 | L<Tamino::Tran::Prepared>, | ||
| 707 | L<Tamino::Tran::Cursor> | ||
| 708 | by saying: | ||
| 709 | |||
| 710 | $obj->xml_twig_class("My::XML::Twig"); | ||
| 711 | $obj->xml_twig_elt_class("My::XML::Twig::Elt"); | ||
| 712 | |||
| 713 | $obj->prepared_class("My::Tamino::Tran::Prepared"); | ||
| 714 | $obj->cursor_class("My::Tamino::Tran::Cursor"); | ||
| 715 | |||
| 716 | where $obj can be an object, so changes are made to that object, | ||
| 717 | or 'Tamino::Tran' - class name, so changes are made class-wide, excepting existing objects. | ||
| 718 | |||
| 719 | =cut | ||
| 720 | |||
| 721 | package timer; | ||
| 722 | use Time::HiRes qw/gettimeofday/; | ||
| 723 | |||
| 724 | sub new { | ||
| 725 | my $class = shift; | ||
| 726 | return bless [ sprintf("%d.%06d", gettimeofday), @_ ], $class; | ||
| 727 | } | ||
| 728 | |||
| 729 | sub close { | ||
| 730 | my $self = shift; | ||
| 731 | my $t = sprintf("%d.%06d", gettimeofday) - shift @$self; | ||
| 732 | $$_ += $t for @$self; | ||
| 733 | @$self = (); | ||
| 734 | return $t; | ||
| 735 | } | ||
| 736 | |||
| 737 | sub DESTROY { | ||
| 738 | my $self = shift; | ||
| 739 | return unless(@$self); | ||
| 740 | my $t = sprintf("%d.%06d", gettimeofday) - shift @$self; | ||
| 741 | $$_ += $t for @$self; | ||
| 742 | } | ||
| 743 | |||
| 744 | 1; | ||
| 745 |