| 1 | use strict; | ||
| 2 | |||
| 3 | package Tamino; | ||
| 4 | |||
| 5 | use vars (qw/$VERSION/); | ||
| 6 | |||
| 7 | $VERSION = 0.03; | ||
| 8 | |||
| 9 | use Class::Accessor; | ||
| 10 | use Class::Data::Inheritable; | ||
| 11 | |||
| 12 | use base qw/Class::Accessor Class::Data::Inheritable/; | ||
| 13 | |||
| 14 | use LWP::UserAgent; | ||
| 15 | use Tamino::Tran; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Tamino - Pure Perl Tamino XML DB driver | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Tamino; | ||
| 24 | |||
| 25 | my $tamino_client = Tamino->new( | ||
| 26 | server => '127.0.0.1/tamino' | ||
| 27 | db => 'mydb' | ||
| 28 | ); | ||
| 29 | |||
| 30 | my $t = $tamino_client->begin_tran | ||
| 31 | or die $tamino_client->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 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This is just an API wrapper. | ||
| 45 | This driver is based on L<LWP::UserAgent>, L<XML::Bare>, and inherits from L<Class::Accessor> and L<Class::Data::Inheritable>. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | __PACKAGE__->mk_classdata($_) for qw/tran_class lwp_ua_class/; | ||
| 50 | |||
| 51 | __PACKAGE__->mk_accessors(qw/server db collection user password tran_class error encoding queries queries_time _debug/); | ||
| 52 | __PACKAGE__->mk_accessors(qw/_ua/); | ||
| 53 | |||
| 54 | __PACKAGE__->tran_class('Tamino::Tran'); | ||
| 55 | __PACKAGE__->lwp_ua_class('LWP::UserAgent'); | ||
| 56 | |||
| 57 | =head1 CONSTRUCTOR | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item new | ||
| 62 | |||
| 63 | my $tamino_client = Tamino->new( | ||
| 64 | server => $server, | ||
| 65 | db => $db, | ||
| 66 | %options | ||
| 67 | ); | ||
| 68 | |||
| 69 | B<server> => Tamino server name, without C<http://>, like C<'hostname/tamino'> | ||
| 70 | |||
| 71 | B<db> => database name | ||
| 72 | |||
| 73 | Options: | ||
| 74 | |||
| 75 | B<collection> => collection name (optional) | ||
| 76 | |||
| 77 | B<user> => user name (optional) | ||
| 78 | |||
| 79 | B<password> => user's password (optional) | ||
| 80 | |||
| 81 | B<encoding> => encoding, 'UTF-8' by default | ||
| 82 | |||
| 83 | B<timeout> => timeout for LWP::UserAgent | ||
| 84 | |||
| 85 | B<keep_alive> => keep_alive for LWP::UserAgent | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub new ($) { | ||
| 92 | my $class = shift; | ||
| 93 | my %args = @_; | ||
| 94 | |||
| 95 | $args{server} =~ s!^http://!!i; | ||
| 96 | |||
| 97 | $class = ref $class || $class; | ||
| 98 | my $self = $class->SUPER::new({ | ||
| 99 | tran_class => $args{tran_class} || $class->tran_class, | ||
| 100 | queries => 0, | ||
| 101 | queries_time=> 0, | ||
| 102 | map { $_ => $args{$_} } qw/server db collection user password encoding/, | ||
| 103 | }); | ||
| 104 | |||
| 105 | $self->_ua($class->lwp_ua_class->new( | ||
| 106 | timeout => $args{timeout} || 5, | ||
| 107 | keep_alive => defined$args{keep_alive}?$args{keep_alive}:10, | ||
| 108 | )); | ||
| 109 | |||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _url ($) { | ||
| 114 | my ($self) = @_; | ||
| 115 | my $url = 'http://'; | ||
| 116 | if($self->user) { | ||
| 117 | $url .= $self->user; | ||
| 118 | $url .= ':'.$self->password if($self->password); | ||
| 119 | $url .= '@'; | ||
| 120 | } | ||
| 121 | $url .= $self->server; | ||
| 122 | $url .= "/".$self->db; | ||
| 123 | $url .= "/".$self->collection if($self->collection); | ||
| 124 | return $url; | ||
| 125 | } | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item begin | ||
| 132 | |||
| 133 | my $t = $tamino_client->begin() or die $tamino_client->error; | ||
| 134 | $t->xquery(...); | ||
| 135 | |||
| 136 | Returns a new L<Tamino::Tran> object. The transaction session is not established. | ||
| 137 | All operations are made in non-transactional context. | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub begin ($;@) { | ||
| 144 | my ($self,%opts) = @_; | ||
| 145 | my $class = $self->tran_class; | ||
| 146 | my $tran = $class->new( | ||
| 147 | %opts, | ||
| 148 | ua => $self->_ua, | ||
| 149 | url => $self->_url, | ||
| 150 | tamino => $self, | ||
| 151 | _debug => $self->_debug, | ||
| 152 | _no_connect => 1, | ||
| 153 | ) or $self->error($class->error); | ||
| 154 | |||
| 155 | return $tran; | ||
| 156 | } | ||
| 157 | |||
| 158 | =pod | ||
| 159 | |||
| 160 | =over 4 | ||
| 161 | |||
| 162 | =item begin_tran | ||
| 163 | |||
| 164 | my $t = $tamino_client->begin_tran(%opts) or die $tamino_client->error; | ||
| 165 | $t->xquery(...); | ||
| 166 | |||
| 167 | Returns a new L<Tamino::Tran> object. The transaction session is established. | ||
| 168 | All operations are made in the transaction context. | ||
| 169 | |||
| 170 | All objects created with I<begin()> and I<begin_tran()> methods do their networking | ||
| 171 | with the same L<LWP::UserAgent> object, which is initialized in I<< Tamino->new >> | ||
| 172 | |||
| 173 | %opts may include: | ||
| 174 | |||
| 175 | C<< isolation_level => $level >>, which can be one of: | ||
| 176 | uncommittedDocument | ||
| 177 | committedCommand | ||
| 178 | stableCursor | ||
| 179 | stableDocument | ||
| 180 | stableDocument | ||
| 181 | |||
| 182 | C<< lock_mode => $mode >>, which can be one of: | ||
| 183 | unprotected | ||
| 184 | shared | ||
| 185 | protected | ||
| 186 | |||
| 187 | C<< lock_wait => $wait >>, which can be one of: | ||
| 188 | yes | ||
| 189 | no | ||
| 190 | |||
| 191 | For What-This-All-Means read Tamino Transaction Guide. | ||
| 192 | |||
| 193 | C<< encoding => $enc >> to tell tamino server that you want $enc encoding. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub begin_tran ($;@) { | ||
| 200 | my ($self,%opts) = @_; | ||
| 201 | my $class = $self->tran_class; | ||
| 202 | my $tran = $class->new( | ||
| 203 | %opts, | ||
| 204 | ua => $self->_ua, | ||
| 205 | url => $self->_url, | ||
| 206 | tamino => $self, | ||
| 207 | _debug => $self->_debug, | ||
| 208 | ) or $self->error($class->error); | ||
| 209 | return $tran; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head1 MISC METHODS | ||
| 213 | |||
| 214 | $tamino_client->server('other_server/tamino'); | ||
| 215 | $tamino_client->db('other_db'); | ||
| 216 | $tamino_client->collection('other_collection'); | ||
| 217 | $tamino_client->user('other_user'); | ||
| 218 | $tamino_client->password('his_password'); | ||
| 219 | $tamino_client->encoding('other_encoding'); | ||
| 220 | |||
| 221 | All of the above change setting for only NEWLY created L<Tamino::Tran> objects. | ||
| 222 | |||
| 223 | Note that I<encoding> option only passed to the Tamino DB, this driver does B<nothing> to take care of encoding. | ||
| 224 | |||
| 225 | print $tamino_client->error; | ||
| 226 | |||
| 227 | =head1 SUBCLASSING | ||
| 228 | |||
| 229 | You can subclass I<Tamino> class. | ||
| 230 | You can tell I<Tamino> to use subclassed L<Tamino::Tran> and L<LWP::UserAgent> by saying: | ||
| 231 | |||
| 232 | Tamino->tran_class('My::Tamino::Tran'); | ||
| 233 | $tamino_client->tran_class('My::Tamino::Tran'); | ||
| 234 | |||
| 235 | Tamino->lwp_ua_class('My::LWP::UserAgent'); | ||
| 236 | |||
| 237 | =head1 SEE ALSO | ||
| 238 | |||
| 239 | L<Tamino::Tran> | ||
| 240 | L<XML::Twig> | ||
| 241 | L<XML::Bare> | ||
| 242 | L<LWP::UserAgent> | ||
| 243 | L<Class::Accessor> | ||
| 244 | L<Class::Data::Inheritable> | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | 1; | ||
| 249 |