File Coverage

blib/lib/ODO/Jena/SQL.pm
Criterion Covered Total %
statement 56 82 68.2
branch 12 24 50.0
condition 2 2 100.0
subroutine 8 10 80.0
pod 1 1 100.0
total 79 119 66.3


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2004-2006 IBM Corporation.
3             #
4             # All rights reserved. This program and the accompanying materials
5             # are made available under the terms of the Eclipse Public License v1.0
6             # which accompanies this distribution, and is available at
7             # http://www.eclipse.org/legal/epl-v10.html
8             #
9             # File: $Source: /var/lib/cvs/ODO/lib/ODO/Jena/SQL.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 10/14/2004
12             # Revision: $Id: SQL.pm,v 1.2 2009-11-25 17:58:25 ubuntu Exp $
13             #
14             # Contributors:
15             # IBM Corporation - initial API and implementation
16             #
17             package ODO::Jena::SQL;
18              
19 1     1   38772 use vars qw /$VERSION/;
  1         3  
  1         288  
20             $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /: (\d+)\.(\d+)/;
21              
22             =head1 NAME
23              
24             ODO::Jena::SQL - Jena SQL related methods
25              
26             =head1 SYNOPSIS
27              
28             use ODO::Jena::SQL;
29              
30             =head1 DESCRIPTION
31              
32             Description.
33              
34             =head1 METHODS
35              
36             =over
37              
38             =item find_sql_library_file( $dbh )
39              
40             =cut
41              
42             sub find_sql_library_file {
43 0     0 1 0 my ($self, $dbh, @more_search_dirs) = @_;
44            
45 0 0       0 @more_search_dirs = ()
46             unless(scalar(@more_search_dirs) > 0);
47            
48 0         0 my @data_sources = $dbh->data_sources();
49              
50 0         0 my $db_type = 'generic_generic';
51 0         0 foreach my $dsn (@data_sources) {
52 0 0       0 if($dsn =~ /^DBI:(\w+):.*$/) {
53 0         0 $db_type = lc($1);
54 0         0 last;
55             }
56             }
57            
58 0         0 my $sql_lib_file = 'ODO/Jena/etc/' . $db_type . '.sql';
59            
60 0         0 foreach my $inc_dir (@more_search_dirs, @INC) {
61 0 0       0 if(-e $inc_dir . '/' . $sql_lib_file) {
62 0         0 $sql_lib_file = $inc_dir . '/' . $sql_lib_file;
63 0         0 last;
64             }
65             }
66            
67 0         0 return $sql_lib_file;
68             }
69              
70             =back
71              
72             =cut
73              
74             1;
75              
76             package ODO::Jena::SQL::Library;
77              
78 1     1   6 use strict;
  1         3  
  1         34  
79 1     1   4 use warnings;
  1         2  
  1         43  
80              
81 1     1   5 use base qw/SQL::Library/;
  1         2  
  1         929  
82              
83 1     1   1369 use ODO::Exception;
  1         4  
  1         775  
84              
85             our $DEBUG = 0;
86              
87             =head1 NAME
88              
89             ODO::Jena::SQL::Library - Load SQL statements in the Jena file format
90              
91             =head1 SYNOPSIS
92              
93             use ODO::Jena::SQL;
94              
95             =head1 DESCRIPTION
96              
97             Description.
98              
99             =head1 METHODS
100              
101             =over
102              
103             =item new( \%options )
104              
105             =cut
106              
107             sub new {
108 3     3   982 my ($package, $options) = @_;
109            
110 3         10 my $self = {
111             'options'=> $options,
112             'contents'=> undef
113             };
114            
115 3         14 $self->{'contents'} = $package->__load_jena_sql_lib($self->{'options'}->{'lib'});
116            
117 3         13 return bless $self, $package;
118             }
119              
120              
121             =item retr($name, @variables)
122              
123             =cut
124              
125             sub retr {
126 6     6   2881 my ($self, $name, @variables) = @_;
127            
128             return undef
129 6 50       19 unless(exists($self->{'contents'}->{ $name }));
130              
131 6         36 my @var_names = ('a' .. 'z');
132            
133 6         6 my @sql_entity = @{ $self->{'contents'}->{ $name } };
  6         14  
134 6         16 while(@variables) {
135 4         6 my $var_value = shift @variables;
136 4         7 my $cur_var_name = shift @var_names;
137            
138 4         7 map { $_ =~ s/\${$cur_var_name}/$var_value/g; } @sql_entity;
  4         53  
139             }
140            
141 6         31 return join('', @sql_entity);
142             }
143              
144              
145             =item __load_jena_sql_lib( )
146              
147             Loads the SQL statements necessary for interacting with the database.
148              
149             Parameters:
150             $filename - Required. The filename to load the SQL from.
151              
152             Returns:
153             A HASH ref of the operations and their associated SQL statement(s)
154              
155             =cut
156              
157             sub __load_jena_sql_lib {
158 3     3   5 my ($self, $filename) = @_;
159            
160 3 50       144 throw ODO::Exception::File::Missing(error=> "Unable to open file: $filename")
161             unless(open(SQL_FILE, $filename));
162            
163 3         7 my $op_name;
164             my @sql_op;
165            
166 0         0 my %operations;
167 3         4 my $line_num = 0;
168            
169 3         7 $/ = "\n";
170 3         56 while() {
171            
172 68 50       109 print STDERR $_
173             if($DEBUG);
174            
175 68         70 $line_num++;
176            
177             next
178 68 100       244 if($_ =~ /^[#]/);
179            
180             # Start a new operation
181 12 100       30 if($_ =~ /^[\n\r]+$/) {
182             # Save the final operation
183 3 50       6 unless($op_name) {
184 0 0       0 print STDERR "WARNING: operationName is not defined at line #$line_num\n"
185             if($DEBUG);
186            
187 0         0 next;
188             }
189            
190 3   100     4 push @{ $operations{ $op_name } }, (join('', @sql_op) || '');
  3         22  
191            
192             # Reset to a blank operation
193 3         4 @sql_op = ();
194 3         9 $op_name = undef;
195             }
196             else {
197             # Check to see if this is a new operation or a continuation
198 9 100       15 if($op_name) {
199            
200 4         5 push @sql_op, $_ ;
201            
202             # If this is the end of a multiline SQL operation,
203             # add it to the SQL statement list
204 4 100       27 if($_ =~ /;;$/) {
205 1         2 push @{ $operations{ $op_name } }, join('', @sql_op);
  1         4  
206            
207 1         4 @sql_op = ();
208             }
209             }
210             else {
211             # Start the new operation
212 5         15 s/[ \r\n]//g;
213            
214 5         9 $op_name = $_;
215            
216             # Initializae the operations SQL statement array
217 5         32 $operations{ $op_name } = [];
218             }
219              
220             } #end if/else
221            
222             } # end while
223            
224 3         24 close(SQL_FILE);
225            
226 3         11 return \%operations;
227             }
228              
229              
230             sub __get_sql_lib_coderefs {
231 0     0     my ($self, $operations) = @_;
232            
233             # Parse and substitute the specialized values in to the SQL statements and build
234             # a dispatch table in ODO::DB::SQLOperations::Interface
235 0           my $sql_lib_coderefs = {};
236            
237 0           foreach my $op_name (keys(%{ $operations } ) ) {
  0            
238 0           my $process = " my \@lines = q( @{ $operations->{ $op_name } } );\n foreach my \$var ( 'a'..'z' ) {\n my \$param = shift \@_;\n last unless(\$param);\n map {\n \$_ =~ s/\\\${\$var}/\$param/g;\n } \@lines; }\n";
  0            
239            
240 0           my $function = "sub {\n shift;\n $process return join(\"\\n\", \@lines);\n}";
241            
242 0           $sql_lib_coderefs->{ $op_name } = $function;
243             }
244            
245 0           return $sql_lib_coderefs;
246             }
247              
248             =back
249              
250             =head1 AUTHOR
251              
252             IBM Corporation
253              
254             =head1 SEE ALSO
255              
256             L, L
257              
258             =head1 COPYRIGHT
259              
260             Copyright (c) 2004-2006 IBM Corporation.
261              
262             All rights reserved. This program and the accompanying materials
263             are made available under the terms of the Eclipse Public License v1.0
264             which accompanies this distribution, and is available at
265             http://www.eclipse.org/legal/epl-v10.html
266              
267             =cut
268              
269             1;
270              
271             __END__