File Coverage

blib/lib/Siesta/DBI.pm
Criterion Covered Total %
statement 29 30 96.6
branch 7 10 70.0
condition n/a
subroutine 5 5 100.0
pod 1 2 50.0
total 42 47 89.3


line stmt bran cond sub pod time code
1 18     18   98 use strict;
  18         43  
  18         899  
2             package Siesta::DBI;
3 18     18   98 use Siesta::Config;
  18         30  
  18         396  
4 18     18   98 use base 'Class::DBI::BaseDSN';
  18         27  
  18         17453  
5             __PACKAGE__->set_db( 'Main', @Siesta::Config::STORAGE );
6             __PACKAGE__->mk_classdata('load_alias');
7              
8             =head1 NAME
9              
10             Siesta::DBI - base class extending Class::DBI
11              
12             =head1 DESCRIPTION
13              
14             most things in the system will be a subclass of Siesta::DBI, giving
15              
16             =head1 METHODS
17              
18             =head2 load_alias( $name )
19              
20             Sets the load_alias of a class to be $name. This is used in
21             conjunction with ->load to provide a shortcut to a serialised item.
22              
23             =head2 load( $id_or_name )
24              
25             Attempt to retrieve an object using either it's well-known name or the
26             row id.
27              
28             It's used like so:
29              
30             package User;
31             use base 'Siesta::DBI';
32             __PACKAGE__->load_alias( 'email' );
33              
34             # time passes ...
35             my $user = User->load( 'jay@front-of.quick.stop';
36              
37             =cut
38              
39             sub load {
40 35     35 1 4057568 my $class = shift;
41 35         100 my $id = shift;
42 35 50       247 if ($id =~ /^\d+$/) {
43 0         0 return $class->retrieve($id);
44             }
45 35         231 my ($item) = $class->search( $class->load_alias => $id );
46 35 100       67409 return unless $item;
47 28         3442 return $item;
48             }
49              
50              
51             =head1 init_db
52              
53             run the database creation script that lives in __DATA__
54              
55             =cut
56              
57             sub init_db {
58 2     2 0 7 my $class = shift;
59 2         10 my $dbh = $class->db_Main;
60 2         36200 my $sql = join ( '', () );
61              
62 2         33 for my $statement (split /;/, $sql) {
63 14 50       565 if ($dbh->{Driver}{Name} eq 'SQLite') {
64 14         139 $statement =~ s/auto_increment//g;
65 14         388 $statement =~ s/,?FOREIGN .*$//mg;
66 14         66 $statement =~ s/TYPE=INNODB//g;
67             }
68 14         101 $statement =~ s/\#.*$//mg; # strip # comments
69 14 100       89 next unless $statement =~ /\S/;
70 12         19 eval { $dbh->do($statement) };
  12         97  
71 12 50       549349 die "$@: $statement" if $@;
72             }
73 2         18 return 1;
74             }
75              
76              
77             1;
78             __DATA__