| 1 | # | ||||||||||
| 2 | # $Id: Simple.pm,v 1.14 2007-01-27 13:35:02 gomor Exp $ | ||||||||||
| 3 | # | ||||||||||
| 4 | package DBIx::SQLite::Simple; | ||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | our $VERSION = '0.35'; | |||||||||
| 10 | |||||||||||
| 11 | 5 | require DBI; | |||||||||
| 12 | 5 | require Class::Gomor::Array; | |||||||||
| 13 | 5 | our @ISA = qw(Class::Gomor::Array); | |||||||||
| 14 | |||||||||||
| 15 | 5 | our @AS = qw( | |||||||||
| 16 | db | ||||||||||
| 17 | _dbh | ||||||||||
| 18 | ); | ||||||||||
| 19 | 5 | __PACKAGE__->cgBuildIndices; | |||||||||
| 20 | 5 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | |||||||||
| 21 | |||||||||||
| 22 | 5 | our $Dbo; | |||||||||
| 23 | |||||||||||
| 24 | =head1 NAME | ||||||||||
| 25 | |||||||||||
| 26 | DBIx::SQLite::Simple - easy access to SQLite databases using objects | ||||||||||
| 27 | |||||||||||
| 28 | =head1 ATTRIBUTES | ||||||||||
| 29 | |||||||||||
| 30 | =over 4 | ||||||||||
| 31 | |||||||||||
| 32 | =item B<db> | ||||||||||
| 33 | |||||||||||
| 34 | Used to store the filename containing the SQLite database. | ||||||||||
| 35 | |||||||||||
| 36 | =back | ||||||||||
| 37 | |||||||||||
| 38 | =head1 METHODS | ||||||||||
| 39 | |||||||||||
| 40 | =over 4 | ||||||||||
| 41 | |||||||||||
| 42 | =item B<new>(db => 'filename.db') | ||||||||||
| 43 | |||||||||||
| 44 | Object creator. Takes one argument, and sets the global variable $Dbo to the newly created database handler. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 49 | 3 | my $self = shift->SUPER::new(@_); | |||||||||
| 50 | |||||||||||
| 51 | 3 | ▶ | confess('Usage: new(db => $db)') unless $self->db; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 3 | ▶ | my $dbh = DBI->connect( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 'dbi:SQLite:dbname='. $self->db, | ||||||||||
| 55 | '', '', | ||||||||||
| 56 | { | ||||||||||
| 57 | RaiseError => 0, | ||||||||||
| 58 | PrintError => 0, | ||||||||||
| 59 | PrintWarn => 0, | ||||||||||
| 60 | AutoCommit => 0, | ||||||||||
| 61 | }, | ||||||||||
| 62 | ) or croak("new: ". $DBI::errstr); | ||||||||||
| 63 | |||||||||||
| 64 | 3 | $self->_dbh($dbh); | |||||||||
| 65 | |||||||||||
| 66 | 3 | $Dbo = $self; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =item B<commit> | ||||||||||
| 70 | |||||||||||
| 71 | Changes made on created database are not automatically commited. You must call this method if you want to commit pending changes. | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 8 | ▶ | sub commit { | ||||||||
|
Subroutine
commit: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 76 | 8 | my $self = shift; | |||||||||
| 77 | 8 | ▶ | $self->_dbh->commit if $self->_dbh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | =item B<close> | ||||||||||
| 81 | |||||||||||
| 82 | When you're done using the database, you can disconnect from it. This method will not commit changes, so do it before closing. | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | sub close { | ||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 87 | 0 | my $self = shift; | |||||||||
| 88 | 0 | ▶ | $self->_dbh->disconnect if $self->_dbh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $self->_dbh(undef); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||
| 93 | 0 | my $self = shift; | |||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | if ($self->_dbh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $self->commit; | |||||||||
| 97 | 0 | $self->close; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | =back | ||||||||||
| 102 | |||||||||||
| 103 | =head1 AUTHOR | ||||||||||
| 104 | |||||||||||
| 105 | Patrice E<lt>GomoRE<gt> Auffret | ||||||||||
| 106 | |||||||||||
| 107 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 108 | |||||||||||
| 109 | Copyright (c) 2005-2015, Patrice E<lt>GomoRE<gt> Auffret | ||||||||||
| 110 | |||||||||||
| 111 | You may distribute this module under the terms of the Artistic license. | ||||||||||
| 112 | See LICENSE.Artistic file in the source distribution archive. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 5 | 1; | |||||||||