Core/Storage/Postgres.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 360 CC 26 SCAR 40.6
CC
26
Cov
0%
_buildSelect
182.0
_getStmt
12.0
removeStmt
12.0
CRAP 57.9
1 #
2 # The contents of this file are subject to the Mozilla Public
3 # License Version 1.1 (the "License"); you may not use this file
4 # except in compliance with the License. You may obtain a copy of
5 # the License at http://www.mozilla.org/MPL/
6 #
7 # Software distributed under the License is distributed on an "AS
8 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 # implied. See the License for the specific language governing
10 # rights and limitations under the License.
11 #
12 # The Original Code is the RDF::Core module
13 #
14 # The Initial Developer of the Original Code is Ginger Alliance Ltd.
15 # Portions created by Ginger Alliance are
16 # Copyright (C) 2001 Ginger Alliance Ltd.
17 # All Rights Reserved.
18 #
19 # Contributor(s):
20 #
21 # Alternatively, the contents of this file may be used under the
22 # terms of the GNU General Public License Version 2 or later (the
23 # "GPL"), in which case the provisions of the GPL are applicable
24 # instead of those above. If you wish to allow use of your
25 # version of this file only under the terms of the GPL and not to
26 # allow others to use your version of this file under the MPL,
27 # indicate your decision by deleting the provisions above and
28 # replace them with the notice and other provisions required by
29 # the GPL. If you do not delete the provisions above, a recipient
30 # may use your version of this file under either the MPL or the
31 # GPL.
32 #
33  
34 package RDF::Core::Storage::Postgres;
35  
36 use strict;
37 require Exporter;
38  
39 our @ISA = qw(RDF::Core::Storage);
40  
41 use Carp;
42 use DBI qw(:sql_types);
43 require RDF::Core::Storage;
44 require RDF::Core::Enumerator::Postgres;
45  
46 ############################################################
47 # constants
48  
49 #use constant RDF_CORE_UNDEFINED => 0;
50  
51 #use constant RDF_CORE_DB_DONT_CREATE => 0;
52 use constant RDF_CORE_DB_CREATE => 1;
53  
54 #use constant RDF_CORE_SELECT_OBJECT_RES => 1;
55 #use constant RDF_CORE_SELECT_OBJECT_LIT => 2;
56  
57 #use constant RDF_CORE_SELECT_DATA => 1;
58 #use constant RDF_CORE_EXISTS_ONLY => 2;
59  
60 #use constant RDF_CORE_COUNT_ONLY => 1;
61 #use constant RDF_CORE_DATA => 2;
62  
63  
64 ############################################################
65 # constructor
66  
67 sub new {
68 my ($class, %params) = @_;
69 $class = ref $class || $class;
70 my $self = {
71 dbconn => $params{ConnectStr},
72 dbuser => $params{DBUser},
73 dbpasswd => $params{DBPassword},
74 model => $params{Model},
75 };
76 bless $self, $class;
77 return $self;
78 }
79  
80 ############################################################
81 # internal private methods
82  
83 sub _getDBHandle {
84 my $self = shift;
85 unless ($self->{dbh}) {
86 $self->{dbh} = DBI->connect($self->{dbconn}, $self->{dbuser},
87 $self->{dbpasswd}, {RaiseError => 1});
88 my $sth = $self->{dbh}->prepare('set client_encoding to unicode');
89 $sth->execute();
90 }
91 return $self->{dbh};
92 }
93  
94 sub _getModelId {
95 my ($self) = @_;
96 unless ($self->{model_id}) {
97 my $sth = $self->_getDBHandle()->prepare('select rdf_model_get(?,?)');
98 $sth->bind_param(1,$self->{model});
99 $sth->bind_param(2,1);
100 $sth->execute;
101 my @row = $sth->fetchrow_array;
102 $self->{model_id} = $row[0];
103 };
104 return $self->{model_id};
105 }
106  
107 sub _getStmt {
108 my ($self, $stmt, $create) = @_;
109 my $rval;
110 my $isLiteral = $stmt->getObject()->isLiteral();
111 my $proc = $isLiteral ?
112 'select rdf_stmt_get(?,?,?,?,?,?,?,?,?)' :
113 'select rdf_stmt_get(?,?,?,?,?,?,?,?)';
114 my $sth = $self->_getDBHandle()->prepare($proc);
115 my $i = 1;
116 $sth->bind_param($i++, $self->_getModelId);
117 $sth->bind_param($i++, $stmt->getSubject()->getNamespace());
118 $sth->bind_param($i++, $stmt->getSubject()->getLocalValue());
119 $sth->bind_param($i++, $stmt->getPredicate()->getNamespace());
120 $sth->bind_param($i++, $stmt->getPredicate()->getLocalValue());
121 if ($isLiteral) {
122 $sth->bind_param($i++, $stmt->getObject()->getValue());
123 $sth->bind_param($i++, $stmt->getObject()->getLang());
124 $sth->bind_param($i++, $stmt->getObject()->getDatatype());
125 } else {
126 $sth->bind_param($i++,$stmt->getObject()->getNamespace());
127 $sth->bind_param($i++,$stmt->getObject()->getLocalValue());
128 };
129 $sth->bind_param($i++, $create);
130 $sth->execute();
131 my @row = $sth->fetchrow_array;
132 return $row[0];
133 }
134  
135 sub _buildSelect {
136 my ($self, $subj, $pred, $obj, %switches) = @_;
137 #apropriate switches are {count} (and {exists} ?)
138  
139 my $sql;
140 my @bindings;
141 #build select part
142 my $select;
143 if ($switches{count}) {
144 $select = "Select count(*)\n"
145 } elsif ($switches{exists}) {
146 $select = "Select exists (Select 1 \n";
147 } else {
148 $select = "Select n1.namespace, r1.local_name, n2.namespace, ";
149 $select .= "r2.local_name, n3.namespace, r3.local_name, s.object_lit,";
150 $select .= "s.object_lang, s.object_type\n"
151 }
152  
153 #build from and where part
154 my $from = "From rdf_statement s ";
155 my $where = "\nWhere s.model_id = ?";
156 push @bindings, $self->_getModelId;
157 if (($obj && !$obj->isLiteral) ||
158 !($switches{count} || $switches{exists})) {
159 $from .= "\nLeft Join rdf_resource r3 On r3.res_id = s.object_res ";
160 $from .= "\nLeft Join rdf_namespace n3 On n3.ns_id = r3.ns_id ";
161
162 }
163 if ($subj || !($switches{count} || $switches{exists})) {
164 $from .= ",\n rdf_resource r1, rdf_namespace n1 ";
165 $where .= "\n and s.subject = r1.res_id ";
166 $where .= "\n and r1.ns_id = n1.ns_id ";
167 }
168 if ($pred || !($switches{count} || $switches{exists})) {
169 $from .= ",\n rdf_resource r2, rdf_namespace n2 ";
170 $where .= "\n and s.predicate = r2.res_id ";
171 $where .= "\n and r2.ns_id = n2.ns_id ";
172 }
173 ##
174 if ($subj) {
175 $where .= "\n and r1.local_name = ? and n1.namespace = ? ";
176 push @bindings, $subj->getLocalValue;
177 push @bindings, $subj->getNamespace;
178 }
179 if ($pred) {
180 $where .= "\n and r2.local_name = ? and n2.namespace = ? ";
181 push @bindings, $pred->getLocalValue;
182 push @bindings, $pred->getNamespace;
183 }
184 if ($obj) {
185 if ($obj->isLiteral) {
186 $where .= "\n and s.object_lit = ? ";
187 push @bindings, $obj->getValue;
188 if (defined $obj->getLang) {
189 $where .= "\n and s.object_lang = ? ";
190 push @bindings, $obj->getLang;
191 } else {
192 $where .= "\n and s.object_lang is null ";
193 }
194 if (defined $obj->getDatatype) {
195 $where .= "\n and s.object_type = ? ";
196 push @bindings, $obj->getDatatype;
197 } else {
198 $where .= "\n and s.object_type is null ";
199 }
200 } else {
201 $where .= "\n and r3.local_name = ? and n3.namespace = ? ";
202 push @bindings, $obj->getLocalValue;
203 push @bindings, $obj->getNamespace;
204 }
205 }
206 $sql = "$select $from $where";
207 $sql .= ")" if $switches{exists};
208 return $sql, \@bindings;
209 }
210  
211 sub _getStmts {
212 my ($self, $subject, $predicate, $object, %switches) = @_;
213  
214 my ($sql, $bindings) = $self->_buildSelect
215 ($subject, $predicate, $object, %switches);
216  
217 my $sth = $self->_getDBHandle()->prepare($sql);
218 $sth->execute(@$bindings);
219 return $sth;
220 }
221  
222  
223 ############################################################
224 # methods
225  
226 sub addStmt {
227 my ($self, $stmt) = @_;
228 return _getStmt ($self, $stmt, RDF_CORE_DB_CREATE);
229 }
230 sub removeStmt {
231 my ($self, $stmt) = @_;
232 my $rval;
233 my $isLiteral = $stmt->getObject()->isLiteral();
234 my $proc = $isLiteral ?
235 'select rdf_stmt_del(?,?,?,?,?,?,?,?)' :
236 'select rdf_stmt_del(?,?,?,?,?,?,?)';
237 my $sth = $self->_getDBHandle()->prepare($proc);
238 my $i = 1;
239 $sth->bind_param($i++, $self->_getModelId);
240 $sth->bind_param($i++, $stmt->getSubject()->getNamespace());
241 $sth->bind_param($i++, $stmt->getSubject()->getLocalValue());
242 $sth->bind_param($i++, $stmt->getPredicate()->getNamespace());
243 $sth->bind_param($i++, $stmt->getPredicate()->getLocalValue());
244 if ($isLiteral) {
245 $sth->bind_param($i++, $stmt->getObject()->getValue());
246 $sth->bind_param($i++, $stmt->getObject()->getLang());
247 $sth->bind_param($i++, $stmt->getObject()->getDatatype());
248 } else {
249 $sth->bind_param($i++,$stmt->getObject()->getNamespace());
250 $sth->bind_param($i++,$stmt->getObject()->getLocalValue());
251 };
252 $sth->execute();
253 my @row = $sth->fetchrow_array;
254 return $row[0];
255 }
256  
257 sub existsStmt {
258 my ($self, $subject, $predicate, $object) = @_;
259 my $dbh = $self->_getDBHandle();
260 unless ($subject || $predicate || $object) {
261 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO OFF');
262 $sth->execute();
263 }
264 my $sth = $self->_getStmts($subject, $predicate, $object, exists=>1);
265 my @row = $sth->fetchrow_array;
266 $dbh = $self->_getDBHandle();
267 unless ($subject || $predicate || $object) {
268 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO ON');
269 $sth->execute();
270 }
271 return $row[0];
272 }
273  
274 sub getStmts {
275 my ($self, $subject, $predicate, $object) = @_;
276 my $dbh = $self->_getDBHandle();
277 unless ($subject || $predicate || $object) {
278 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO OFF');
279 $sth->execute();
280 }
281  
282 my $sth = $self->_getStmts($subject, $predicate, $object);
283 unless ($subject || $predicate || $object) {
284 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO ON');
285 $sth->execute();
286 }
287 return new RDF::Core::Enumerator::Postgres( (Cursor => $sth) );
288 }
289  
290 sub countStmts {
291 my ($self, $subject, $predicate, $object) = @_;
292  
293 my $dbh = $self->_getDBHandle();
294 unless ($subject || $predicate || $object) {
295 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO OFF');
296 $sth->execute();
297 }
298 my $sth = $self->_getStmts($subject, $predicate, $object, count=>1);
299 my @row = $sth->fetchrow_array;
300 unless ($subject || $predicate || $object) {
301 my $sth = $dbh->prepare('SET ENABLE_SEQSCAN TO ON');
302 $sth->execute();
303 }
304 return $row[0];
305 }
306  
307 sub getNewResourceId {
308 my $self = shift;
309 my $sth = $self->_getDBHandle()->prepare('select rdf_res_new_id()');
310 $sth->execute();
311 my @row = $sth->fetchrow_array;
312 return $row[0];
313 }
314  
315 sub DESTROY {
316 my $self = shift;
317 $self->{dbh}->disconnect() if ($self->{dbh});
318 };
319  
320 1;
321 __END__