Toolkit/Setup/SQLite.pm

filter: Statementstmt 20.3% 22 / 108 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 41.6% 5 / 12 Podpod 66.6% 2 / 3 totaltotal 14.0% 29 / 206 CC 37 SCAR 48.3
CC
37
Cov
12%
setup
306.0
_get_dbh
42.0
_disconnect_required
42.0
CRAP 124.8
« Toolkit/Setup/Pg.pm ↑ Summary Toolkit/Feed/RSS.pm »
1 package Wiki::Toolkit::Setup::SQLite;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use vars qw( @ISA $VERSION $SCHEMA_VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Wiki::Toolkit::Setup::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 @ISA = qw( Wiki::Toolkit::Setup::Database );
10 4 $VERSION = '0.11';
11  
12 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 $SCHEMA_VERSION = $VERSION*100;
16  
17 4 my $create_sql = {
18 10 => {
19 schema_info => [ qq|
20 CREATE TABLE schema_info (
21 version integer NOT NULL default 0
22 );
23 |, qq|
24 INSERT INTO schema_info VALUES (10)
25 | ],
26  
27 node => [ qq|
28 CREATE TABLE node (
29 id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
30 name varchar(200) NOT NULL DEFAULT '',
31 version integer NOT NULL default 0,
32 text mediumtext NOT NULL default '',
33 modified datetime default NULL,
34 moderate boolean NOT NULL default '0'
35 )
36 |, qq|
37 CREATE UNIQUE INDEX node_name ON node (name)
38 | ],
39 content => [ qq|
40 CREATE TABLE content (
41 node_id integer NOT NULL,
42 version integer NOT NULL default 0,
43 text mediumtext NOT NULL default '',
44 modified datetime default NULL,
45 comment mediumtext NOT NULL default '',
46 moderated boolean NOT NULL default '1',
47 verified datetime default NULL,
48 verified_info mediumtext NOT NULL default '',
49 PRIMARY KEY (node_id, version)
50 )
51 | ],
52 internal_links => [ qq|
53 CREATE TABLE internal_links (
54 link_from varchar(200) NOT NULL default '',
55 link_to varchar(200) NOT NULL default '',
56 PRIMARY KEY (link_from, link_to)
57 )
58 | ],
59 metadata => [ qq|
60 CREATE TABLE metadata (
61 node_id integer NOT NULL,
62 version integer NOT NULL default 0,
63 metadata_type varchar(200) NOT NULL DEFAULT '',
64 metadata_value mediumtext NOT NULL DEFAULT ''
65 )
66 | ]
67 },
68 11 => {
69 schema_info => [ qq|
70 CREATE TABLE schema_info (
71 version integer NOT NULL default 0
72 );
73 |, qq|
74 INSERT INTO schema_info VALUES (11)
75 | ],
76  
77 node => [ qq|
78 CREATE TABLE node (
79 id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
80 name varchar(200) NOT NULL DEFAULT '',
81 version integer NOT NULL default 0,
82 text mediumtext NOT NULL,
83 modified datetime default NULL,
84 moderate boolean NOT NULL default '0'
85 )
86 |, qq|
87 CREATE UNIQUE INDEX node_name ON node (name)
88 | ],
89 content => [ qq|
90 CREATE TABLE content (
91 node_id integer NOT NULL,
92 version integer NOT NULL default 0,
93 text mediumtext NOT NULL,
94 modified datetime default NULL,
95 comment mediumtext,
96 moderated boolean NOT NULL default '1',
97 verified datetime default NULL,
98 verified_info mediumtext,
99 PRIMARY KEY (node_id, version)
100 )
101 | ],
102 internal_links => [ qq|
103 CREATE TABLE internal_links (
104 link_from varchar(200) NOT NULL default '',
105 link_to varchar(200) NOT NULL default '',
106 PRIMARY KEY (link_from, link_to)
107 )
108 | ],
109 metadata => [ qq|
110 CREATE TABLE metadata (
111 node_id integer NOT NULL,
112 version integer NOT NULL default 0,
113 metadata_type varchar(200) NOT NULL DEFAULT '',
114 metadata_value mediumtext NOT NULL
115 )
116 | ]
117 },
118 };
119  
120 4 my %fetch_upgrades = (
121 '10_to_11' => 1,
122 );
123  
124 4 my %upgrades = ();
125  
126 =head1 NAME
127  
128 Wiki::Toolkit::Setup::SQLite - Set up tables for a Wiki::Toolkit store in a SQLite database.
129  
130 =head1 SYNOPSIS
131  
132 use Wiki::Toolkit::Setup::SQLite;
133 Wiki::Toolkit::Setup::SQLite::setup( $dbfile );
134  
135 =head1 DESCRIPTION
136  
137 Set up a SQLite database for use as a Wiki::Toolkit store.
138  
139 =head1 FUNCTIONS
140  
141 =over 4
142  
143 =item B<setup>
144  
145 use Wiki::Toolkit::Setup::SQLite;
146  
147 Wiki::Toolkit::Setup::SQLite::setup( $filename );
148  
149 or
150  
151 Wiki::Toolkit::Setup::SQLite::setup( $dbh );
152  
153 Takes one argument - B<either> the name of the file that the SQLite
154 database is stored in B<or> an active database handle.
155  
156 B<NOTE:> If a table that the module wants to create already exists,
157 C<setup> will leave it alone. This means that you can safely run this
158 on an existing L<Wiki::Toolkit> database to bring the schema up to date
159 with the current L<Wiki::Toolkit> version. If you wish to completely start
160 again with a fresh database, run C<cleardb> first.
161  
162 An optional second argument may be passed specifying the schema version
163 to use; this is B<ONLY> intended to be used during unit testing and should
164 not normally be specified.
165  
166 =cut
167  
168 0 ▶sub setup {
Subroutine setup: not called CC=17 SCAR=57.2
Pod documented
169 0 my @args = @_;
170 0 my $dbh = _get_dbh( @args );
171 0 my $disconnect_required = _disconnect_required( @args );
172 0 ▶ my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION;
Condition0%0/3
_get_wanted_schema(@args) || $Wiki::Toolkit::Setup::SQLite::SCHEMA_VERSION
l!l&&r!l&&!r
000
MC/DC0%0/2
_get_wanted_schema(@args) || $Wiki::Toolkit::Setup::SQLite::SCHEMA_VERSION
_get_wanted_schema(@args) $Wiki::Toolkit::Setup::SQLite::SCHEMA_VERSION
Truth table0%0/3
A || B
A= _get_wanted_schema(@args)B= $Wiki::Toolkit::Setup::SQLite::SCHEMA_VERSION
ABresult
1X1
011
000
173  
174 die "No schema information for requested schema version $wanted_schema\n"
175 0 ▶ unless $create_sql->{$wanted_schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $create_sql->{$wanted_schema} 0 0 0
176  
177 # Check whether tables exist, set them up if not.
178 0 my %tables = fetch_tables_listing($dbh, $wanted_schema);
179  
180 # Do we need to upgrade the schema?
181 # (Don't check if no tables currently exist)
182 0 my $upgrade_schema;
183 my @cur_data;
184 0 ▶ if(scalar keys %tables > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %tables > 0) 0 0 0
185 0 $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema);
186 }
187 0 ▶ if($upgrade_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upgrade_schema) 0 0 0
188 0 ▶ if ($fetch_upgrades{$upgrade_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetch_upgrades{$upgrade_schema}) 0 0 0
189 # Grab current data
190 0 print "Upgrading: $upgrade_schema\n";
191 0 @cur_data = eval("&Wiki::Toolkit::Setup::Database::fetch_upgrade_".$upgrade_schema."(\$dbh)");
192  
193 # Drop the current tables
194 0 cleardb($dbh);
195  
196 # Grab new list of tables
197 0 %tables = fetch_tables_listing($dbh, $wanted_schema);
198 }
199 }
200  
201 # Set up tables if not found
202 0 foreach my $required ( keys %{$create_sql->{$wanted_schema}} ) {
203 0 ▶ if ( $tables{$required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tables{$required}) { } 0 0 0
204 0 print "Table $required already exists... skipping...\n";
205 } else {
206 0 print "Creating table $required... done\n";
207 0 foreach my $sql (@{$create_sql->{$wanted_schema}->{$required}} ) {
208 0 ▶ $dbh->do($sql) or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($sql) or croak($dbh->errstr) 0 0 0
209 }
210 }
211 }
212  
213 # If upgrading, load in the new data
214 0 ▶ if($upgrade_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upgrade_schema) 0 0 0
215 0 ▶ if ($fetch_upgrades{$upgrade_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetch_upgrades{$upgrade_schema}) { } 0 0 0
216 0 Wiki::Toolkit::Setup::Database::bulk_data_insert($dbh,@cur_data);
217 } else {
218 0 print "Upgrading schema: $upgrade_schema\n";
219 0 my @updates = @{$upgrades{$upgrade_schema}};
220 0 foreach my $update (@updates) {
221 0 ▶ if(ref($update) eq "CODE") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $update eq 'CODE') { } 0 0 0
elsif (ref $update eq 'ARRAY') { } 0 0 0
222 0 &$update($dbh);
223 } elsif(ref($update) eq "ARRAY") {
224 0 foreach my $nupdate (@$update) {
225 0 $dbh->do($nupdate);
226 }
227 } else {
228 0 $dbh->do($update);
229 }
230 }
231 }
232 }
233  
234 # Clean up if we made our own dbh.
235 0 ▶ $dbh->disconnect if $disconnect_required;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect_required 0 0 0
236 }
237  
238 # Internal method - what tables are defined?
239 0 ▶sub fetch_tables_listing {
Subroutine fetch_tables_listing: not called CC=3 SCAR=24.8
Pod undocumented
240 0 my $dbh = shift;
241 0 my $wanted_schema = shift;
242  
243 # Check whether tables exist, set them up if not.
244 my $sql = "SELECT name FROM sqlite_master
245 WHERE type='table' AND name in ("
246 0 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")";
247 0 ▶ my $sth = $dbh->prepare($sql) or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($sql) or croak($dbh->errstr) 0 0 0
248 0 $sth->execute;
249 0 my %tables;
250 0 ▶ while ( my $table = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table = $sth->fetchrow_array) 0 0 0
251 0 $tables{$table} = 1;
252 }
253 0 return %tables;
254 }
255  
256 =item B<cleardb>
257  
258 use Wiki::Toolkit::Setup::SQLite;
259  
260 # Clear out all Wiki::Toolkit tables from the database.
261 Wiki::Toolkit::Setup::SQLite::cleardb( $filename );
262  
263 or
264  
265 Wiki::Toolkit::Setup::SQLite::cleardb( $dbh );
266  
267 Takes one argument - B<either> the name of the file that the SQLite
268 database is stored in B<or> an active database handle.
269  
270 Clears out all L<Wiki::Toolkit> store tables from the database. B<NOTE>
271 that this will lose all your data; you probably only want to use this
272 for testing purposes or if you really screwed up somewhere. Note also
273 that it doesn't touch any L<Wiki::Toolkit> search backend tables; if you
274 have any of those in the same or a different database see
275 L<Wiki::Toolkit::Setup::DBIxFTS> or L<Wiki::Toolkit::Setup::SII>, depending on
276 which search backend you're using.
277  
278 =cut
279  
280 0 ▶sub cleardb {
Subroutine cleardb: not called CC=4 SCAR=30.0
Pod documented
281 0 my @args = @_;
282 0 my $dbh = _get_dbh( @args );
283 0 my $disconnect_required = _disconnect_required( @args );
284  
285 0 print "Dropping tables... ";
286 my $sql = "SELECT name FROM sqlite_master
287 WHERE type='table' AND name in ("
288 0 . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")";
289 0 foreach my $tableref (@{$dbh->selectall_arrayref($sql)}) {
290 0 ▶ $dbh->do("DROP TABLE $tableref->[0]") or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do("DROP TABLE $tableref->[0]") or croak($dbh->errstr) 0 0 0
291 }
292 0 print "done\n";
293  
294 # Clean up if we made our own dbh.
295 0 ▶ $dbh->disconnect if $disconnect_required;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect_required 0 0 0
296 }
297  
298 0 ▶sub _get_dbh {
Subroutine _get_dbh: not called CC=6 SCAR=37.4
299 # Database handle passed in.
300 0 ▶ if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "DBI::db") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0] ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
ABresult
0X0
100
111
301 0 return $_[0];
302 }
303  
304 # Args passed as hashref.
305 0 ▶ if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "HASH") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
306 0 my %args = %{$_[0]};
307 0 ▶ if ( $args{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'dbh'}) { } 0 0 0
308 0 return $args{dbh};
309 } else {
310 0 return _make_dbh( %args );
311 }
312 }
313  
314 # Args passed as list of connection details.
315 0 return _make_dbh( dbname => $_[0] );
316 }
317  
318 0 ▶sub _get_wanted_schema {
Subroutine _get_wanted_schema: not called CC=5 SCAR=34.0
319 # Database handle passed in.
320 0 ▶ if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "DBI::db") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0] ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
ABresult
0X0
100
111
321 0 return undef;
322 }
323  
324 # Args passed as hashref.
325 0 ▶ if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "HASH") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
326 0 my %args = %{$_[0]};
327 0 return $args{wanted_schema};
328 }
329 }
330  
331 0 ▶sub _disconnect_required {
Subroutine _disconnect_required: not called CC=6 SCAR=37.4
332 # Database handle passed in.
333 0 ▶ if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "DBI::db") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "DBI::db"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "DBI::db"
ref $_[0] ref $_[0] eq "DBI::db"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "DBI::db"
ABresult
0X0
100
111
334 0 return 0;
335 }
336  
337 # Args passed as hashref.
338 0 ▶ if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq "HASH") 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
339 0 my %args = %{$_[0]};
340 0 ▶ if ( $args{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'dbh'}) { } 0 0 0
341 0 return 0;
342 } else {
343 0 return 1;
344 }
345 }
346  
347 # Args passed as list of connection details.
348 0 return 1;
349 }
350  
351 0 ▶sub _make_dbh {
Subroutine _make_dbh: not called CC=2 SCAR=17.9
352 0 my %args = @_;
353 0 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=$args{dbname}", "", "",
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect(qq[dbi:SQLite:dbname=$args{"dbname"}], "", "", {"PrintError", 1, "RaiseError", 1, "AutoCommit", 1}) or croak(DBI::errstr()) 0 0 0
354 { PrintError => 1, RaiseError => 1,
355 AutoCommit => 1 } )
356 or croak DBI::errstr;
357 0 return $dbh;
358 }
359  
360 =back
361  
362 =head1 ALTERNATIVE CALLING SYNTAX
363  
364 As requested by Podmaster. Instead of passing arguments to the methods as
365  
366 ($filename)
367  
368 you can pass them as
369  
370 ( { dbname => $filename } )
371  
372 or indeed
373  
374 ( { dbh => $dbh } )
375  
376 Note that's a hashref, not a hash.
377  
378 =head1 AUTHOR
379  
380 Kake Pugh (kake@earth.li).
381  
382 =head1 COPYRIGHT
383  
384 Copyright (C) 2002-2004 Kake Pugh. All Rights Reserved.
385 Copyright (C) 2006-2009 the Wiki::Toolkit team. All Rights Reserved.
386  
387 This module is free software; you can redistribute it and/or modify it
388 under the same terms as Perl itself.
389  
390 =head1 SEE ALSO
391  
392 L<Wiki::Toolkit>, L<Wiki::Toolkit::Setup::DBIxFTS>, L<Wiki::Toolkit::Setup::SII>
393  
394 =cut
395  
396 4 1;
397  
« Toolkit/Setup/Pg.pm ↑ Summary Toolkit/Feed/RSS.pm »