| 1 | 27 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package Siesta::DBI; | ||||||||||
| 3 | 27 | ▶ | use Siesta::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 27 | ▶ | use base 'Class::DBI::BaseDSN'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 27 | __PACKAGE__->set_db( 'Main', @Siesta::Config::STORAGE ); | |||||||||
| 6 | 27 | __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 | 41 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 40 | 41 | my $class = shift; | |||||||||
| 41 | 41 | my $id = shift; | |||||||||
| 42 | 41 | ▶ | if ($id =~ /^\d+$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | return $class->retrieve($id); | |||||||||
| 44 | } | ||||||||||
| 45 | 41 | my ($item) = $class->search( $class->load_alias => $id ); | |||||||||
| 46 | 41 | ▶ | return unless $item; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 34 | 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 | 2 | ▶ | sub init_db { | ||||||||
|
Subroutine
init_db: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 58 | 2 | my $class = shift; | |||||||||
| 59 | 2 | my $dbh = $class->db_Main; | |||||||||
| 60 | 2 | my $sql = join ( '', (<DATA>) ); | |||||||||
| 61 | |||||||||||
| 62 | 2 | for my $statement (split /;/, $sql) { | |||||||||
| 63 | 14 | ▶ | if ($dbh->{Driver}{Name} eq 'SQLite') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 14 | $statement =~ s/auto_increment//g; | |||||||||
| 65 | 14 | $statement =~ s/,?FOREIGN .*$//mg; | |||||||||
| 66 | 14 | $statement =~ s/TYPE=INNODB//g; | |||||||||
| 67 | } | ||||||||||
| 68 | 14 | $statement =~ s/\#.*$//mg; # strip # comments | |||||||||
| 69 | 14 | ▶ | next unless $statement =~ /\S/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 12 | eval { $dbh->do($statement) }; | |||||||||
| 71 | 12 | ▶ | die "$@: $statement" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | 2 | return 1; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | 27 | 1; | |||||||||
| 78 | __DATA__ | ||||||||||