lib/RDF/Service/Interface/DBI/V01.pm untested

filter: Statementstmt 0.0% 0 / 457 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 157 MC/DCmcdc 0.0% 0 / 86 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 834 CC 44 SCAR 42.1
CC
44
Cov
0%
init_types
240.0
store_props
56.0
store_node
42.0
CRAP 67.6
1 # $Id: V01.pm,v 1.29 2000/12/21 22:04:18 aigan Exp $ -*-cperl-*-
2  
3 package RDF::Service::Interface::DBI::V01;
4  
5 #=====================================================================
6 #
7 # DESCRIPTION
8 # Interface to storage and retrieval of statements in a general purpouse DB
9 #
10 # AUTHOR
11 # Jonas Liljegren <jonas@paranormal.se>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2000 Jonas Liljegren. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #=====================================================================
20  
21 use strict;
22 use DBI;
23 #use POSIX;
24 #use Time::HiRes qw( time );
25 use vars qw( $prefix $interface_uri @node_fields );
26 use RDF::Service::Constants qw( :all );
27 use RDF::Service::Cache qw( get_unique_id uri2id id2uri debug $DEBUG );
28 use RDF::Service::Resource;
29 use Data::Dumper;
30 use Carp;
31  
32  
33 $prefix = [ ];
34  
35 # Todo: Decide on a standard way to name functions
36 # # Will not use the long names in this version...
37 $interface_uri = "org.cpan.RDF.Interface.DBI.V01";
38  
39 @node_fields = qw( id uri iscontainer isprefix
40 label aliasfor
41 pred distr subj obj model
42 member
43 isliteral lang value );
44  
45  
46 sub register
47 {
48 my( $i, $args ) = @_;
49  
50 my $connect = $args->{'connect'} or croak "Connection string missing";
51 my $name = $args->{'name'} || "";
52 my $passwd = $args->{'passwd'} || "";
53  
54 my $dbi_options =
55 {
56 RaiseError => 0,
57 };
58  
59 my $dbh = ( DBI->connect_cached( $connect, $name, $passwd, $dbi_options ) );
60  
61  
62 die "Connect to $connect failed\n" unless $dbh;
63  
64 # Maby we should store interface data in a special hash instead,
65 # like interface($interface->[ID])->{'dbh'}... But that seams to
66 # be just as long. Another alternative would be to reserve a
67 # range especially for interfaces.
68 #
69 #
70 # This interface module can be used for connection to several
71 # diffrent DBs. Every such connection will have the same methods
72 # but the method calls will give diffrent results. It is diffrent
73 # interface objects but the same interface module.
74 #
75 debug "Store DBH for $i->[URISTR] in ".
76 "[PRIVATE]{$i->[ID]}{'dbh'}\n", 3;
77  
78 $i->[PRIVATE]{$i->[ID]}{'dbh'} = $dbh;
79  
80 return
81 {
82 '' =>
83 {
84 NS_LS.'#Service' =>
85 {
86 },
87 NS_LS.'#interface' =>
88 {
89 },
90 NS_LS.'#Model' =>
91 {
92 # 'add_arc' => [\&add_arc],
93 'find_arcs_list' => [\&find_arcs_list],
94 },
95 NS_RDFS.'Resource' =>
96 {
97 'init_types' => [\&init_types],
98 'init_rev_subjs' => [\&init_rev_subjs],
99 'init_rev_objs' => [\&init_rev_objs],
100 'name' => [\&name],
101 'find_node' => [\&find_node],
102 'store_types' => [\&store_types],
103 'store_props' => [\&store_props],
104 'store_node' => [\&store_node],
105 'remove' => [\&remove],
106 'remove_types' => [\&remove_types],
107 'remove_props' => [\&remove_props],
108 },
109 NS_RDFS.'Class' =>
110 {
111 'objects_list' => [\&objects_list],
112 'init_rev_types' => [\&init_rev_types],
113 },
114 },
115 };
116 }
117  
118  
119  
120 sub find_node
121 {
122 my( $self, $i, $uristr ) = @_;
123 #
124 # Is the node contained in the model?
125  
126 my $p = {}; # Interface private data
127 my $obj;
128  
129 # Look for the URI in the DB.
130 #
131 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
132  
133 my $sth = $dbh->prepare_cached("
134 select id, refid, refpart, hasalias from uri
135 where string=?
136 ");
137 $sth->execute( $uristr );
138  
139 my( $r_id, $r_refid, $r_refpart, $r_hasalias );
140 $sth->bind_columns(\$r_id, \$r_refid, \$r_refpart, \$r_hasalias);
141 if( $sth->fetch )
142 {
143 $p->{'uri'} = $r_id;
144  
145 $obj = $self->get( $uristr );
146 $obj->[NODE][PRIVATE]{$i->[ID]} = $p;
147 }
148 $sth->finish; # Release the handler
149  
150 return( $obj, 1 ) if defined $obj;
151 return( undef, 0 );
152 }
153  
154 sub find_arcs_list
155 {
156 my( $self, $i, $pred, $subj, $obj ) = @_;
157 #
158 # TODO: This will primarly return explicit arcs. But should also
159 # return many implicit arcs. Fo not return type arcs.
160  
161 die "Not implemented";
162 }
163  
164 sub name
165 {
166 # Will give the part of the URI following the 'namespace'
167 die "not implemented";
168 }
169  
170 sub add_arc ## DEPRECATED
171 {
172 my( $self, $i, $uristr, $pred, $subj, $obj ) = @_;
173  
174 # Why is this needed? Use store_props!
175  
176 die "deprecated";
177  
178 # Assuems that the arc does not exist
179  
180 my $arc = $self->declare_arc($pred,
181 $subj,
182 $obj,
183 $uristr,
184 );
185  
186  
187 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
188  
189  
190 # TODO: Only do this the first time
191 #
192 my $field_str = join ", ", @node_fields;
193 my $place_str = join ", ", ('?')x @node_fields;
194  
195 my $sth = $dbh->prepare_cached(" insert into node
196 ($field_str)
197 values ($place_str)
198 ");
199  
200  
201 # TODO: Handle if arc already exist
202 # my %p = %{$self->[NODE][PRIVATE]{$i->[ID]}};
203  
204 my %p = ();
205 $p{'id'} ||= &_nextval($dbh);
206  
207 $p{'uri'} = &_create_uri( $arc->uri, $i);
208 $p{'iscontainer'} = 'false';
209 $p{'isprefix'} = 'false';
210 $p{'label'} = undef;
211 $p{'aliasfor'} = undef;
212 $p{'pred'} = &_get_id( $arc->pred, $i);
213 $p{'distr'} = 'false';
214 $p{'subj'} = &_get_id( $arc->subj, $i);
215 $p{'obj'} = &_get_id( $arc->obj, $i);
216 $p{'model'} = &_get_id( $arc->[WMODEL][NODE], $i);
217 $p{'member'} = undef;
218 $p{'isliteral'} = 'false';
219 $p{'lang'} = undef;
220 $p{'value'} = undef;
221  
222  
223 $sth->execute( map $p{$_}, @node_fields )
224 or confess( $sth->errstr );
225  
226 return( 1, 1 );
227 }
228  
229 sub init_rev_subjs
230 {
231 my( $self, $i, $constraint ) = @_;
232  
233 # This should initiate all props from this interface
234  
235  
236 # TODO: Use the constraint
237  
238 $self->[NODE][TYPE_ALL] or $self->init_types;
239 $self->[NODE][TYPE_ALL] ||= 1;
240  
241 # TODO: Should props be undef if type changes?
242  
243 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
244  
245 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
246  
247 # TODO: Also read all the other node data
248  
249 my $sth = $dbh->prepare_cached("
250 select auri.string as arc,
251 puri.string as pred,
252 suri.string as subj,
253 ouri.string as obj,
254 muri.string as model
255 from node,
256 uri auri,
257 uri puri,
258 uri suri,
259 uri ouri,
260 uri muri
261 where node.pred = puri.id and
262 node.subj = suri.id and
263 node.obj = ouri.id and
264 node.model = muri.id and
265 node.uri = auri.id and
266 suri.string = ?
267 ");
268  
269 $sth->execute( $self->[NODE][URISTR] );
270 my $tbl = $sth->fetchall_arrayref({});
271 $sth->finish;
272  
273 debug "Fetching props\n", 1;
274 foreach my $r ( @$tbl )
275 {
276 my $pred = $self->get( $r->{'pred'} );
277 my $subj = $self;
278 my $obj = $self->get( $r->{'obj'} );
279 my $model = $self->get( $r->{'model'} )->[NODE];
280 debug "..Found a $pred->[NODE][URISTR]\n", 1;
281  
282 $subj->declare_add_prop( $pred, $obj, $r->{'arc'}, $model, 1 );
283 }
284  
285  
286 return( 1, 3 );
287 }
288  
289 sub init_rev_objs
290 {
291 my( $self, $i, $constraint ) = @_;
292  
293 # This should get all rev_props from this interface
294  
295  
296 # TODO: Use the constraint
297  
298  
299 $self->[NODE][TYPE_ALL] or $self->init_types;
300 $self->[NODE][TYPE_ALL] ||= 1;
301  
302 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
303  
304 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
305  
306 # TODO: Also read all the other node data
307  
308 my $sth = $dbh->prepare_cached("
309 select auri.string as arc,
310 puri.string as pred,
311 suri.string as subj,
312 ouri.string as obj,
313 muri.string as model
314 from node,
315 uri auri,
316 uri puri,
317 uri suri,
318 uri ouri,
319 uri muri
320 where node.pred = puri.id and
321 node.subj = suri.id and
322 node.obj = ouri.id and
323 node.model = muri.id and
324 node.uri = auri.id and
325 ouri.string = ?
326 ");
327  
328 # warn "*** $self->[NODE][URISTR]\n";
329  
330 $sth->execute( $self->[NODE][URISTR] );
331 my $tbl = $sth->fetchall_arrayref({});
332 $sth->finish;
333  
334 debug "Fetching rev_props\n", 1;
335 foreach my $r ( @$tbl )
336 {
337 my $pred = $self->get( $r->{'pred'} );
338 my $subj = $self->get( $r->{'subj'} );
339 my $obj = $self;
340 my $model = $self->get( $r->{'model'} );
341 debug "..Found a $pred->[NODE][URISTR]\n", 1;
342  
343 $subj->declare_add_prop( $pred, $obj, $r->{'arc'}, $model, 1 );
344 }
345  
346 return( 1, 3 );
347 }
348  
349 sub init_types
350 {
351 my( $self, $i ) = @_;
352 #
353 # Read the types from the DBI. Get all info from the node
354 # record
355  
356 # TODO: Get the implicite types from subClassOf (Handled by
357 # Base/V01)
358  
359 if( $DEBUG )
360 {
361 debug "Init types for $self->[NODE][URISTR]\n", 2;
362  
363 unless( ref $self eq "RDF::Service::Context" )
364 {
365 die "Wrong type for self: $self";
366 }
367  
368 unless( ref $i eq "RDF::Service::Resource" )
369 {
370 die "Wrong type for i: $i";
371 }
372  
373 die "No node for self" unless $self->[NODE];
374  
375 die "No private for self_node" unless $self->[NODE][PRIVATE];
376 }
377  
378 # Look for the URI in the DB.
379 #
380 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
381 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
382 $p->{'uri'} ||= &_get_id($self->[NODE], $i);
383  
384 my $node = $self->[NODE];
385  
386 Node:
387 {
388 # TODO: Reuse cols variable and sth
389 my @cols = qw( id iscontainer isprefix label aliasfor
390 model pred distr subj obj member isliteral
391 lang value blob );
392  
393 my $fields = join ", ", @cols;
394  
395 my $sth_node = $dbh->prepare_cached("
396 select $fields
397 from node
398 where uri=?
399 ");
400  
401 my $true = '1';
402 my $false = '0';
403  
404 $sth_node->execute( $p->{'uri'} );
405 my $tbl = $sth_node->fetchall_arrayref({});
406 $sth_node->finish; # The fetchall should finish the sth implicitly
407  
408 # TODO: Handle the case with more than one hit!
409  
410 foreach my $r ( @$tbl )
411 {
412 debug "Changing SOLID to 1 for $node->[URISTR] ".
413 "IDS $node->[IDS]\n", 3;
414 $node->[SOLID] = 1; # Resource found in db
415 my $types = [];
416  
417 # TODO: Go through all the varables
418  
419 # iscontainer
420  
421 # isprefix
422  
423 # label (there can be only one!)
424 if( $r->{'label'} )
425 {
426 if( $node->[LABEL] )
427 {
428 $node->[LABEL] .= " / $r->{'label'}";
429 }
430 else
431 {
432 $node->[LABEL] = $r->{'label'};
433 }
434 }
435  
436 # aliasfor
437  
438 # model
439 my $model_res = &_get_node($r->{'model'}, $self, $i);
440 my $model = $model_res->[NODE];
441 $self->[NODE][MODEL] = $model;
442 if( $DEBUG )
443 {
444 unless( $model_res->is_a( NS_LS.'#Model' ) )
445 {
446 die "The model is not a model";
447 }
448 }
449  
450 # pred distr subj obj
451 if( my $r_pred = $r->{'pred'} )
452 {
453 push @$types, NS_RDF.'Statement';
454 }
455  
456 # member
457  
458 # isliteral, lang, value, blob
459 if( $r->{'isliteral'} eq $true )
460 {
461 debug "..Literal: $self->[NODE][URISTR]\n", 2;
462 if( $r->{'value'} )
463 {
464 # Rewrite from $r->{'value'}
465 $self->[NODE][VALUE] = \${$r}{'value'};
466 push @$types, NS_RDFS.'Literal';
467  
468 if( $DEBUG )
469 {
470 unless( ref $self->[NODE][VALUE] eq 'SCALAR' )
471 {
472 die "Value not a string ( $self->[NODE][VALUE] ) ";
473 }
474 }
475 }
476 else
477 {
478 die "not implemented";
479 }
480 }
481 $self->declare_add_types( $types, $model, 1 );
482 }
483 }
484  
485  
486 Types:
487 {
488 my $sth_types = $dbh->prepare_cached("
489 select type.id, string, type, model
490 from type, uri
491 where node=? and uri.id=type
492 ");
493  
494 $sth_types->execute( $p->{'uri'} );
495 my $tbl = $sth_types->fetchall_arrayref({});
496 $sth_types->finish;
497 foreach my $r ( @$tbl )
498 {
499 my $type = $self->get($r->{'string'});
500 my $model = &_get_node( $r->{'model'}, $self, $i )->[NODE];
501  
502 # Remember the record ID
503 $type->[NODE][PRIVATE]{$i->[ID]}{'uri'} = $r->{'type'};
504  
505 # TODO: Maby group the types before creating them
506 $self->declare_add_types( [$type], $model, 1 );
507 }
508 }
509  
510 debug "Types for $self->[NODE][URISTR]\n", 1;
511 debug $self->types_as_string, 1;
512  
513 return( 1, 3 );
514 }
515  
516 sub init_rev_types
517 {
518 my( $self, $i ) = @_;
519 #
520 # Read the types from the DBI.
521  
522 # TODO: Get the implicite types from subClassOf. ( Should be
523 # handled by declare_add_rev_types )
524  
525 # I may the assumption that this initiation does not affect
526 # knowledge of the resource SOLID state.
527  
528  
529 # Look for the URI in the DB.
530 #
531 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
532 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
533 $p->{'uri'} ||= &_get_id($self->[NODE], $i);
534  
535 my $rev_types = [];
536  
537 my $sth_rev_types = $dbh->prepare_cached("
538 select type.id, string, node, model
539 from type, uri
540 where type=? and uri.id=node
541 ");
542  
543 $sth_rev_types->execute( $p->{'uri'} );
544 my $tbl = $sth_rev_types->fetchall_arrayref({});
545 $sth_rev_types->finish;
546 foreach my $r ( @$tbl )
547 {
548 my $rev_type = $self->get($r->{'string'});
549 my $model = &_get_node( $r->{'model'}, $self, $i )->[NODE];
550  
551 # Remember the record ID
552 $rev_type->[NODE][PRIVATE]{$i->[ID]}{'uri'} = $r->{'node'};
553  
554 # TODO: Group the rev_types (by model) before creating them
555 $rev_type->declare_add_types( [$self], $model, 1 );
556 }
557  
558 return( 1, 3 );
559 }
560  
561  
562 sub remove
563 {
564 my( $self, $i ) = @_;
565  
566 # Remove node from interface. But not from the cahce. This is
567 # called from Base delete before it removes the node from cache.
568  
569 # TODO: Check that the node (with the model) actually exist in
570 # this interface
571  
572  
573 # Remove types and node
574  
575 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
576  
577 my $sth_type = $dbh->prepare_cached("
578 delete from type
579 where node = ? and model = ?");
580 my $sth_node = $dbh->prepare_cached("
581 delete from node
582 where uri = ? and model = ?");
583  
584 my $r_model = &_get_id( $self->[WMODEL][NODE], $i );
585 my $r_node = &_get_id( $self->[NODE], $i );
586 my $node_p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
587  
588 $sth_type->execute( $r_node, $r_model)
589 or confess( $sth_type->errstr );
590 $sth_node->execute( $r_node, $r_model)
591 or confess( $sth_type->errstr );
592  
593 debug "Deleted $self->[NODE][URISTR] for model ".
594 $self->[WMODEL][NODE][URISTR]."\n", 1;
595  
596 # Remove the private information. This removes info for all
597 # models. Not just the deleted one.
598  
599 # TODO: Check that there is no mixup between diffrent models
600 # interface private data in the same node.
601  
602 delete $self->[NODE][PRIVATE]{$i->[ID]};
603  
604 # TODO: What happens if the resource is stored in several
605 # interfaces? When should we set SOLID to false?
606  
607 return( 1, 3 );
608 }
609  
610 sub store_types
611 {
612 my( $self, $i ) = @_;
613 #
614 # TODO: Could store duplicate type statements. But only from
615 # diffrent models.
616  
617 my $node = $self->[NODE];
618  
619 debug $self->types_as_string, 2;
620  
621  
622 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
623 my $p = $node->[PRIVATE]{$i->[ID]} || {};
624  
625 my $sth = $dbh->prepare_cached("
626 insert into type
627 (node, type, model)
628 values (?, ?, ?)
629 ");
630  
631 my $r_node = &_get_id($node, $i);
632  
633 foreach my $type_id ( keys %{$node->[TYPE]} )
634 {
635 my $type = $self->get_context_by_id( $type_id );
636 my $r_type = &_get_id($type->[NODE], $i);
637  
638 debug "..Checking $type->[NODE][URISTR]\n", 2;
639  
640 $type->store; # Store the type if necessary
641  
642 foreach my $model_id ( keys %{$node->[TYPE]{$type_id}} )
643 {
644 # TODO: Use _get_id_by_node_id
645 my $model = $self->get_context_by_id( $model_id )->[NODE];
646 debug "....Model $model->[URISTR]\n", 2;
647  
648 # Don't store type if it's already solid
649 if( $node->[TYPE]{$type_id}{$model_id} == 2 )
650 {
651 my $uri = &id2uri( $type_id );
652 debug " Already solid: $uri\n", 1;
653 next;
654 }
655  
656 debug " Saving type in DB\n", 2;
657  
658 my $r_model = &_get_id($model, $i);
659 $sth->execute( $r_node, $r_type, $r_model )
660 or confess( $sth->errstr );
661  
662 # Type is now solid!
663 $node->[TYPE]{$type_id}{$model_id} = 2;
664 }
665 }
666  
667 # This interface store all the types. Do not continue
668 return( 1, 1 );
669 }
670  
671 sub remove_types
672 {
673 my( $self, $i, $types ) = @_;
674  
675 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
676  
677 my $sth = $dbh->prepare_cached("
678 delete from type
679 where node=? and type=? and model=?
680 ");
681  
682 my $r_node = &_get_id($self->[NODE], $i);
683 my $r_model = &_get_id($self->[WMODEL][NODE], $i);
684  
685 foreach my $type ( @$types )
686 {
687 debug " t $type->[NODE][URISTR]\n", 2;
688  
689 my $r_type = &_get_id($type->[NODE], $i);
690 $sth->execute( $r_node, $r_type, $r_model )
691 or confess( $sth->errstr );
692 }
693  
694 return( 1, 3 );
695 }
696  
697 sub store_props
698 {
699 my( $self, $i ) = @_;
700 #
701 # The supplied preds are a list of pred_uri. They specify the
702 # preds to store. The arcs are already declared. Store the arcs
703 # matching the WMODEL. Implicit preds should not be included in
704 # the $preds list. Preds already stored should not be included.
705  
706 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
707 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
708 my $node = $self->[NODE];
709  
710 my $sth = $dbh->prepare_cached("
711 insert into node
712 (id, uri, iscontainer, isprefix, model,
713 pred, subj, obj, isliteral)
714 values (?, ?, false, false, ?, ?, ?, ?, false)
715 ");
716  
717 my $r_subj = $p->{'uri'} ||= &_get_id($node, $i);
718  
719 # TODO: Store the subj (like update_node)
720  
721 if( $DEBUG )
722 {
723 my $pred_cnt = keys %{$node->[REV_SUBJ]};
724 # debug " *****************************\n", 1;
725 debug " Resource has $pred_cnt predicates\n", 1;
726  
727 # foreach my $obj ( @{$self->arc_obj_list( NS_LS.'#updated' )} )
728 # {
729 # debug " u $obj->[NODE][URISTR]\n";
730 # }
731 }
732  
733 foreach my $pred_id ( keys %{$node->[REV_SUBJ]} )
734 {
735 my $pred = $self->get_context_by_id($pred_id);
736 my $r_pred = &_get_id($pred->[NODE], $i);
737  
738 $pred->store; # Store the pred if necessary
739  
740 if( $DEBUG )
741 {
742 debug "..Storing $pred->[NODE][URISTR] ($pred_id)\n", 1;
743 debug "....".@{$node->[REV_SUBJ]{$pred_id}}." entries\n", 1;
744 }
745  
746 foreach my $arc_node ( @{$node->[REV_SUBJ]{$pred_id}} )
747 {
748 debug "....Checking arc $arc_node->[URISTR]\n", 3;
749 # Don't store prop is it's already solid
750 next if $arc_node->[SOLID];
751  
752 if( $DEBUG )
753 {
754 if( $arc_node->[OBJ][VALUE] )
755 {
756 unless( ref($arc_node->[OBJ][VALUE]) eq 'SCALAR')
757 {
758 confess "Bad value for $arc_node->[OBJ][URISTR] ( ".
759 ref($arc_node->[OBJ][VALUE])." ne 'SCALAR' )";
760 }
761 }
762 }
763  
764 my $pa = $arc_node->[PRIVATE]{$i->[ID]} || {};
765  
766 $pa->{'id'} ||= &_nextval($dbh);
767 $pa->{'pred'} = $r_pred;
768 $pa->{'subj'} = $r_subj;
769 $pa->{'obj'} ||= &_get_id( $arc_node->[OBJ], $i );
770 $pa->{'uri'} ||= &_get_id( $arc_node, $i );
771 $pa->{'model'} ||= &_get_id( $arc_node->[MODEL], $i );
772  
773 $sth->execute( $pa->{'id'}, $pa->{'uri'}, $pa->{'model'},
774 $r_pred, $r_subj, $pa->{'obj'} )
775 or confess( $sth->errstr );
776  
777 # The arc has been saved.
778 debug "Changing SOLID to 1 for $arc_node->[URISTR] ".
779 "IDS $arc_node->[IDS]\n", 3;
780 $arc_node->[SOLID] = 1;
781  
782 # Instead of $arc->obj
783 #
784 my $obj = $self->new($arc_node->[OBJ]);
785 debug "....Checking obj $obj->[NODE][URISTR]\n", 2;
786 $obj->store; # Store the obj if necessary
787  
788 debug "....Stored arc $arc_node->[URISTR]\n", 1;
789 }
790 }
791  
792 # This interface store all the props. Do not continue
793 return( 1, 1 );
794 }
795  
796  
797 sub store_node
798 {
799 my( $self, $i ) = @_;
800 #
801 # Store the object in the database
802  
803 my $node = $self->[NODE];
804  
805 die "Not implemented" if $node->[MULTI];
806  
807 # Should we update, create or ignore the node?
808 #
809 # TODO: Handle other special data
810 #
811 if( $node->[PRED] or $node->[VALUE] or
812 $node->[LABEL] or $node->[MEMBER] )
813 {
814 my $p = $node->[PRIVATE]{$i->[ID]} || {};
815 my $node_exist = $p->{'id'};
816 unless( $node_exist )
817 {
818 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
819 my $sth = $dbh->prepare_cached("
820 select id
821 from node
822 where uri=?
823 ");
824 $p->{'uri'} ||= &_get_id($self->[NODE], $i);
825 $sth->execute( $p->{'uri'} );
826 $node_exist = 1 if $sth->rows;
827 $sth->finish;
828 }
829  
830 if( $node_exist )
831 {
832 &_update_node($self, $i);
833 }
834 else
835 {
836 &_create_node($self, $i);
837 }
838 }
839 else
840 {
841 debug "..The node is neither Literal nor arc!\n", 4;
842 }
843  
844 # The resource is now stored and SOLID
845 #
846 debug "Changing SOLID to 1 for $node->[URISTR] ".
847 "IDS $node->[IDS]\n", 3;
848 $node->[SOLID] = 1;
849  
850 return( 1, 1);
851 }
852  
853 sub _update_node
854 {
855 my( $self, $i ) = @_;
856 # This only updates the node; not the types or properties. Mainly
857 # used to update literals
858  
859 # TODO: What shall we do about multipple models?
860  
861  
862 my $p = $self->[NODE][PRIVATE]{$i->[ID]} || {};
863 my %p = %$p;
864 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
865  
866  
867 # TODO: Only do this the first time
868 #
869 my $field_str = join ", ", map "$_=?",
870 @node_fields[1..$#node_fields];
871  
872 my $sth = $dbh->prepare_cached(" update node
873 set $field_str
874 where uri = ?
875 and model = ?
876 ");
877  
878 $p{'uri'} ||= &_get_id( $self->[NODE], $i) or die;
879 $p{'iscontainer'} = 'false';
880 $p{'isprefix'} = 'false';
881 $p{'label'} = $self->[NODE][LABEL];
882 $p{'aliasfor'} ||= &_get_id( $self->[NODE][ALIASFOR], $i);
883 $p{'pred'} ||= &_get_id( $self->[NODE][PRED], $i);
884 $p{'distr'} = 'false';
885 $p{'subj'} ||= &_get_id( $self->[NODE][SUBJ], $i);
886 $p{'obj'} ||= &_get_id( $self->[NODE][OBJ], $i);
887  
888 # TODO: What should the new model be?
889 $p{'model'} ||= &_get_id( $self->[WMODEL][NODE], $i) or die;
890  
891 $p{'member'} ||= &_get_id( $self->[NODE][MEMBER], $i);
892  
893 # TODO: Use isa(literal)
894 if( $self->[NODE][VALUE] )
895 {
896 if( $DEBUG )
897 {
898 ref $self->[NODE][VALUE] eq 'SCALAR' or
899 die "Value not a string";
900 }
901  
902 $p{'isliteral'} = 'true';
903 $p{'lang'} = undef;
904 if( length(${$self->[NODE][VALUE]}) <= 250 )
905 {
906 $p{'value'} = ${$self->[NODE][VALUE]};
907 }
908 else
909 {
910 die "not implemented";
911 }
912 }
913 else
914 {
915 $p{'isliteral'} = 'false';
916 }
917  
918  
919 debug "Updating value to ($p{'value'})\n", 2;
920 debug ".. where uri=$p{'uri'} and model=$p{'model'}\n", 2;
921  
922  
923 $sth->execute( map $p{$_}, @node_fields[1..$#node_fields],
924 'uri', 'model' )
925 or confess( $sth->errstr );
926  
927 $self->[NODE][PRIVATE]{$i->[ID]} = \%p;
928  
929 return( 1, 3 );
930 }
931  
932 sub _create_node
933 {
934 my( $self, $i ) = @_;
935 #
936 # Stores the object in the database. The object does not exist
937 # before this. All data gets stored in the supplied $model.
938  
939 debug "_create_node $self->[NODE][URISTR]\n", 2;
940  
941 my $model = $self->[WMODEL][NODE];
942 my $node = $self->[NODE];
943  
944 # Interface PRIVATE data. These has to be updated then the
945 # corresponding official data change. The dependencies could be
946 # handled as they are (will be) in RDF::Cache
947 #
948 my $p = $node->[PRIVATE]{$i->[ID]} || {};
949 my %p = %$p;
950  
951 debug "Getting DBH for $i->[URISTR] from ".
952 "[PRIVATE]{$i->[ID]}{'dbh'}\n", 3;
953 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
954  
955  
956 # TODO: Only do this the first time
957 #
958 my $field_str = join ", ", @node_fields;
959 my $place_str = join ", ", ('?')x @node_fields;
960  
961 my $sth = $dbh->prepare_cached(" insert into node
962 ($field_str)
963 values ($place_str)
964 ");
965  
966 # This is a new node. We know that it doesn't exist yet. Create a
967 # new record in the db
968 #
969 $p{'id'} ||= &_nextval($dbh) or die;
970  
971 # TODO: method calls should be used, i case the attribute hasn't
972 # been initialized. $self->pred->private($i, 'id')? It's possible
973 # that the attribute object is stored in several interfaces. We
974 # are only intrested in the private id for this interface. We
975 # can't make a special method for getting that id, because we
976 # can't guarantee that another interface doesn't have the same
977 # method. The private() method could be constructed to access a
978 # specific attribute, but that doesn't seem to be much better than
979 # just using the _get_id() function.
980 #
981 # I don't like this repetivity there we get the
982 # sth and execute it once for each resource. How much can we save
983 # by group the lookups together?
984 #
985 # The list below could be shortend if we knew the type of node to
986 # create.
987 #
988 $p{'uri'} ||= &_create_uri( $node->[URISTR], $i) or die;
989 $p{'iscontainer'} = 'false';
990 $p{'isprefix'} = 'false';
991 $p{'label'} = $node->[LABEL];
992 $p{'aliasfor'} ||= &_get_id( $node->[ALIASFOR], $i);
993 $p{'pred'} ||= &_get_id( $node->[PRED], $i);
994 $p{'distr'} = 'false';
995 $p{'subj'} ||= &_get_id( $node->[SUBJ], $i);
996 $p{'obj'} ||= &_get_id( $node->[OBJ], $i);
997 $p{'model'} ||= &_get_id( $model, $i) or die;
998 $p{'member'} ||= &_get_id( $node->[MEMBER], $i);
999 if( $node->[VALUE] )
1000 {
1001 if( $DEBUG )
1002 {
1003 ref $node->[VALUE] eq 'SCALAR' or
1004 die "Value not a string: ( $node->[VALUE] )";
1005 }
1006  
1007 $p{'isliteral'} = 'true';
1008 $p{'lang'} = undef;
1009 if( length(${$node->[VALUE]}) <= 250 )
1010 {
1011 $p{'value'} = ${$node->[VALUE]};
1012 }
1013 else
1014 {
1015 die "not implemented";
1016 }
1017 }
1018 else
1019 {
1020 $p{'isliteral'} = 'false';
1021 }
1022  
1023 debug ".. id: $p{'id'}\n", 1;
1024 debug "..uri: $p{'uri'}\n", 1;
1025  
1026 # confess "SQL insert node $node->[URISTR]\n" if $DEBUG;
1027  
1028 $node->[PRIVATE]{$i->[ID]} = \%p;
1029  
1030 $sth->execute( map $p{$_}, @node_fields )
1031 or confess( $sth->errstr );
1032 }
1033  
1034 sub _get_node
1035 {
1036 my( $r_id, $caller, $i ) = @_;
1037 #
1038 # find_node_by_interface_node_id
1039  
1040  
1041 # TODO: Optimize with a interface id cache
1042  
1043 # Look for the URI in the DB.
1044 #
1045 my $dbh = $i->[PRIVATE]{$i->[ID]}{'dbh'};
1046 my $p = {}; # Interface private data
1047 my $obj;
1048 $p->{'id'} = $r_id;
1049  
1050 my $sth = $dbh->prepare_cached("
1051 select string, refid, refpart, hasalias from uri
1052 where id=?
1053 ");
1054 $sth->execute( $r_id );
1055  
1056 my( $r_uristr, $r_refid, $r_refpart, $r_hasalias );
1057 $sth->bind_columns(\$r_uristr, \$r_refid, \$r_refpart, \$r_hasalias);
1058 if( $sth->fetch )
1059 {
1060 $obj = $caller->get( $r_uristr );
1061 $obj->[NODE][PRIVATE]{$i->[ID]} = $p;
1062 }
1063 $sth->finish; # Release the handler
1064  
1065 die "couldn't find the resource with record id $r_id" unless $obj;
1066  
1067 return $obj;
1068 }
1069  
1070 sub _get_id
1071 {
1072 return undef unless defined $_[0]; # Common case
1073 my( $obj_node, $interface ) = @_;
1074 #
1075 # The object already exist. Here we just want to know what id it
1076 # has in the DB. NB!!! field URI in NODE table.
1077  
1078 if( $DEBUG )
1079 {
1080 debug "_get_id( $obj_node->[URISTR] )\n", 2;
1081 unless( ref $obj_node eq "RDF::Service::Resource" )
1082 {
1083 confess "obj_node $obj_node malformed ";
1084 }
1085 }
1086  
1087 # Has the object a known connection to the DB?
1088 #
1089 my $p = $obj_node->[PRIVATE]{$interface->[ID]} || {};
1090 if( defined( my $id = $p->{'uri'}) )
1091 {
1092 return $id;
1093 }
1094  
1095  
1096 $obj_node->[URISTR] or die "No URI supplied";
1097  
1098 # Look for the URI in the DB.
1099 #
1100 my $dbh = $interface->[PRIVATE]{$interface->[ID]}{'dbh'};
1101  
1102 my $sth = $dbh->prepare_cached("
1103 select id, refid, refpart, hasalias from uri
1104 where string=?
1105 ");
1106 $sth->execute( $obj_node->[URISTR] );
1107  
1108 my( $r_id, $r_refid, $r_refpart, $r_hasalias );
1109 $sth->bind_columns(\$r_id, \$r_refid, \$r_refpart, \$r_hasalias);
1110 if( $sth->fetch )
1111 {
1112 $p->{'uri'} = $r_id;
1113 $sth->finish; # Release the handler
1114  
1115 # TODO: Maby update other data with the result?
1116 return $r_id;
1117 }
1118 else
1119 {
1120 $sth->finish; # Release the handler
1121  
1122 # If URI not found in DB:
1123 #
1124 # Insert the uri in the DB. The object itself doesn't have to be
1125 # inserted since it would already be in the DB if this interface
1126 # handles its storage.
1127  
1128 $p->{'uri'} = &_create_uri( $obj_node->[URISTR], $interface );
1129 $obj_node->[PRIVATE]{$interface->[ID]} = $p;
1130 return $p->{'uri'};
1131 }
1132 }
1133  
1134 sub _create_uri
1135 {
1136 my( $uri, $interface ) = @_;
1137 #
1138 # Insert a new URI in the DB.
1139  
1140 debug "_create_uri( $uri )\n", 2;
1141  
1142 # Same as _get_id(), except that we know that the uri doesn't
1143 # exist in the db. No error checking.
1144  
1145 my $dbh = $interface->[PRIVATE]{$interface->[ID]}{'dbh'};
1146  
1147 my $sth = $dbh->prepare_cached("
1148 insert into uri
1149 (string, id, hasalias)
1150 values (?,?,false)
1151 ");
1152 my $id = &_nextval($dbh, 'uri_id_seq');
1153 $sth->execute($uri, $id);
1154 die unless defined $id;
1155  
1156 return $id;
1157 }
1158  
1159 sub _nextval
1160 {
1161 my( $dbh, $seq ) = @_;
1162  
1163 # Values could be collected before they are needed, as to save the
1164 # lookup time.
1165  
1166 $seq ||= 'node_id_seq';
1167 my $sth = $dbh->prepare_cached( "select nextval(?)" );
1168 $sth->execute( $seq );
1169 my( $id ) = $sth->fetchrow_array;
1170 $sth->finish;
1171  
1172 $id or die "Failed to get nextval";
1173 }
1174  
1175 1;