line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Net::MarkLogic::XDBC; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Net::MarkLogic::XDBC - XDBC connectivity for MarkLogic CIS servers. |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Net::MarkLogic::XDBC |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$xdbc = Net::MarkLogic::XDBC->new( "user:pass@localhost:9000" ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$xdbc = Net::MarkLogic::XDBC->new(host => $host, |
15
|
|
|
|
|
|
|
port => $port, |
16
|
|
|
|
|
|
|
username => $user, |
17
|
|
|
|
|
|
|
password => $pass, ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$result = $agent->query($xquery); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
print $result->content; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
@items = $result->items; |
24
|
|
|
|
|
|
|
print $item->content; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 DESCRIPTION |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Alpha. API will change. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Connect to a CIS XDBC server and execute xquery code. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
|
22804
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
35
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
1
|
|
3757
|
use Data::Dumper; |
|
1
|
|
|
|
|
14527
|
|
|
1
|
|
|
|
|
81
|
|
38
|
1
|
|
|
1
|
|
2087
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
76879
|
|
|
1
|
|
|
|
|
35
|
|
39
|
1
|
|
|
1
|
|
1321
|
use Class::Accessor; |
|
1
|
|
|
|
|
2949
|
|
|
1
|
|
|
|
|
6
|
|
40
|
1
|
|
|
1
|
|
8059
|
use Class::Fields; |
|
1
|
|
|
|
|
50836
|
|
|
1
|
|
|
|
|
430
|
|
41
|
1
|
|
|
1
|
|
12
|
use URI::Escape; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
58
|
|
42
|
1
|
|
|
1
|
|
13369
|
use Template; |
|
1
|
|
|
|
|
120679
|
|
|
1
|
|
|
|
|
38
|
|
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
1
|
|
944
|
use Net::MarkLogic::XDBC::Result; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
10
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
our $VERSION = 0.11; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
our @BASIC_FIELDS = qw(host port username password uri); |
49
|
|
|
|
|
|
|
our @REQUIRED_FIELDS = qw(host port username password); |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
1
|
|
76
|
use base qw(Class::Accessor Class::Fields); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
99
|
|
52
|
1
|
|
|
1
|
|
5
|
use fields @BASIC_FIELDS, qw(ua header template); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
53
|
|
|
|
|
|
|
Net::MarkLogic::XDBC->mk_accessors( @BASIC_FIELDS ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 METHODS |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 new() |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$xdbc = Net::MarkLogic::XDBC->new( "user:pass@localhost:9000" ); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$xdbc = Net::MarkLogic::XDBC->new( host => $hostname, |
63
|
|
|
|
|
|
|
port => $port, |
64
|
|
|
|
|
|
|
username => $user, |
65
|
|
|
|
|
|
|
password => $pass, ); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Connect using a connection string or named host, port, username, and password parameters. |
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub new |
72
|
|
|
|
|
|
|
{ |
73
|
1
|
|
|
1
|
1
|
12
|
my $class = shift; |
74
|
|
|
|
|
|
|
|
75
|
1
|
|
|
|
|
3
|
my %args; |
76
|
|
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
5
|
if (scalar @_ == 1) |
78
|
|
|
|
|
|
|
{ |
79
|
0
|
0
|
|
|
|
0
|
$_[0] =~ m/ |
80
|
|
|
|
|
|
|
^ ([^:]+) # username |
81
|
|
|
|
|
|
|
: ([^\s\@]+) # password |
82
|
|
|
|
|
|
|
\@ ([\w\-\.]+) # hostname |
83
|
|
|
|
|
|
|
: (\d+) $ # port |
84
|
|
|
|
|
|
|
/x or die "Bad connection string: $_[0]"; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
$args{username} = $1; |
87
|
0
|
|
|
|
|
0
|
$args{password} = $2; |
88
|
0
|
|
|
|
|
0
|
$args{host} = $3; |
89
|
0
|
|
|
|
|
0
|
$args{port} = $4; |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
7
|
else { %args = @_; } |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
166
|
foreach my $key (@REQUIRED_FIELDS) { |
94
|
4
|
50
|
|
|
|
12
|
die "Invalid connection info. Missing $key." unless $args{$key}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
33
|
|
|
9
|
my $self = bless ({}, ref ($class) || $class); |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
8
|
$self->host($args{host}); |
100
|
1
|
|
|
|
|
34
|
$self->port($args{port}); |
101
|
1
|
|
|
|
|
12
|
$self->username($args{username}); |
102
|
1
|
|
|
|
|
13
|
$self->password($args{password}); |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
9
|
return ($self); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 query() |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$result = $xdbc->query($xquery); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Execute XQUERY code on XDBC server. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub query |
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
119
|
0
|
|
|
|
|
|
my $xquery = shift; |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
die "Need xquery argument" unless $xquery = uri_escape($xquery); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my $request = HTTP::Request->new("POST", $self->server_uri, |
125
|
|
|
|
|
|
|
$self->header, "xquery=$xquery"); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my $http_response = $self->ua->request($request); |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
my $result = Net::MarkLogic::XDBC::Result->new( |
130
|
|
|
|
|
|
|
response => $http_response |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $result; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 query_from_template() |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$result = $xdbc->query_from_template($template, $args); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Generate XQUERY code from a template toolkit template and arguments, then execute on XDBC server. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This might be overkill, but it's definitely a feature you're not going to find |
143
|
|
|
|
|
|
|
in the Java API. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub query_from_template |
148
|
|
|
|
|
|
|
{ |
149
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
150
|
0
|
|
|
|
|
|
my $template = shift; |
151
|
0
|
|
|
|
|
|
my $args = shift; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
my $t = Template->new(); |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
my $xquery; |
156
|
0
|
|
|
|
|
|
$t->process($template, $args, \$xquery); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
return $self->query($xquery); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
# server_uri() |
161
|
|
|
|
|
|
|
# The XDBC uri, either generated from defaults based on the given host and port |
162
|
|
|
|
|
|
|
# or generated from a supplied uri. |
163
|
|
|
|
|
|
|
sub server_uri |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
166
|
|
|
|
|
|
|
|
167
|
0
|
0
|
|
|
|
|
if ($self->uri) { return $self->uri; } |
|
0
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
else { return "http://" . $self->host . ":" . $self->port . "/eval"; } |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 ATTRIBUTE METHODS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
These methods function as setter/getters for the objects attributes. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
$get = $xdbc->foo(); |
176
|
|
|
|
|
|
|
$xdbc->foo($set); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
These shouldn't be important unless you need to finetune the behavior or tweak |
179
|
|
|
|
|
|
|
the settings. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 ua() |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
LWP::UserAgent, just in case anyone needs to tweak settings. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub ua |
189
|
|
|
|
|
|
|
{ |
190
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
191
|
|
|
|
|
|
|
|
192
|
0
|
0
|
|
|
|
|
$self->{ua} = $_[0] if $_[0]; |
193
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
unless ($self->{ua}) |
195
|
|
|
|
|
|
|
{ |
196
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( agent => |
197
|
|
|
|
|
|
|
"Net::MarkLogic::XDBC/$VERSION MarkXDBC/2.2-1",); |
198
|
0
|
|
|
|
|
|
$self->{ua} = $ua; |
199
|
|
|
|
|
|
|
} |
200
|
0
|
|
|
|
|
|
return $self->{ua}; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 header() |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
HTTP::Headers, sent on every request to the XDBC server. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub header |
210
|
|
|
|
|
|
|
{ |
211
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
212
|
|
|
|
|
|
|
|
213
|
0
|
0
|
|
|
|
|
$self->{header} = $_[0] if $_[0]; |
214
|
|
|
|
|
|
|
|
215
|
0
|
0
|
|
|
|
|
unless ($self->{header}) |
216
|
|
|
|
|
|
|
{ |
217
|
0
|
|
|
|
|
|
my $header = HTTP::Headers->new(); |
218
|
0
|
|
|
|
|
|
$header->authorization_basic( $self->username, $self->password ); |
219
|
0
|
|
|
|
|
|
$self->{header} = $header; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
return $self->{header}; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 host() |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Name or IP address of the XDBC server host. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 port() |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Port number of XDBC server. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 username() |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
User used for authentication. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 password() |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Password used for authentication. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 uri() |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Set a custom URI to connect to the XDBC server. Default connection go to |
244
|
|
|
|
|
|
|
"http://$host:$port/eval". |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 BUGS |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Big time. Watch out for changing APIs. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 AUTHOR |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Tony Stubblebine |
255
|
|
|
|
|
|
|
tonys@oreilly.com |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Code contributions from: Michael Blakeley |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Advice and comments from Raffaele Sena, Ryan Grimm, Andy Bruno. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head1 COPYRIGHT |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Copyright 2004 Tony Stubblebine |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); |
268
|
|
|
|
|
|
|
you may not use this file except in compliance with the License. |
269
|
|
|
|
|
|
|
You may obtain a copy of the License at |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software |
274
|
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, |
275
|
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
276
|
|
|
|
|
|
|
See the License for the specific language governing permissions and |
277
|
|
|
|
|
|
|
limitations under the License. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 SEE ALSO |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
MarkLogic Documentation: |
282
|
|
|
|
|
|
|
http://xqzone.marklogic.com/ |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=cut |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
1; #this line is important and will help the module return a true value |
287
|
|
|
|
|
|
|
__END__ |