aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/share/perl/lib/DBHandler.pm
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--share/perl/lib/DBHandler.pm119
1 files changed, 0 insertions, 119 deletions
diff --git a/share/perl/lib/DBHandler.pm b/share/perl/lib/DBHandler.pm
deleted file mode 100644
index 1435ba2..0000000
--- a/share/perl/lib/DBHandler.pm
+++ /dev/null
@@ -1,119 +0,0 @@
1use strict;
2use DBI;
3use Carp;
4
5package DBHandler;
6
7#our $dbh = undef;
8use vars qw ($DB_CONNECTION);
9
10sub getConnection {
11 my ($dsn, $user, $pass) = @_;
12 #return $DB_CONNECTION if ($DB_CONNECTION);
13 $DB_CONNECTION = DBI->connect($dsn, $user, $pass);
14 $DB_CONNECTION->{AutoCommit} = 1;
15 $DB_CONNECTION->{RaiseError} = 1;
16 return $DB_CONNECTION;
17}
18
19# #############
20# Simple statement
21package Statement;
22
23sub new {
24 my ( $this, $dbh, $sql, $is_trans ) = @_;
25 # @@@ sql should be tested OK, so here just die
26 my $sth = $dbh->prepare($sql) || Carp::croak( $dbh->errstr );
27 my %fields = (
28 dbh => $dbh,
29 sql => $sql,
30 sth => $sth,
31 is_trans => $is_trans,
32 );
33 return bless \%fields, $this;
34}
35
36sub exec {
37 my ( $this, @param ) = @_;
38 my $dbh = $this->{dbh};
39 my $sth = $this->{sth};
40 my $sql = $this->{sql};
41
42 if ( !$sth->execute(@param) ) {
43 if ( $this->{is_trans} ) {
44 $dbh->rollback();
45 }
46 Carp::croak( $dbh->errstr );
47 }
48 my @ret = ();
49 if ( $sql =~ /^select/i ) {
50 # @@@ get result object
51 while ( my $res = $sth->fetchrow_hashref() ) {
52 push @ret, $res;
53 }
54 }
55 # @@@ $sth->finish();
56 return \@ret;
57}
58
59sub last_id {
60 my $this = shift;
61 my $dbh = $this->{dbh};
62 return $dbh->last_insert_id(undef, undef, undef, undef);
63}
64
65sub DESTROY {
66 my $this = shift;
67 my $sth = $this->{sth};
68 $sth->finish();
69}
70
71# #############
72# Transaction
73package Transaction;
74
75my $IS_TRANS = 1;
76
77sub new {
78 my ( $this, $dbh ) = @_;
79 # @@@ fatal error, just die
80 $dbh->begin_work() || Carp::croak( $dbh->errstr );
81 my %fields = (
82 dbh => $dbh,
83 Active => 1,
84 );
85 return bless \%fields, $this;
86}
87
88sub createStatement {
89 my ( $this, $sql) = @_;
90 # @@@ fatal error, just die
91 Carp::croak("transaction not begin") if ( !$this->{Active} );
92 my $dbh = $this->{dbh};
93 return new Statement($dbh, $sql, $IS_TRANS);
94}
95
96sub commit {
97 my $this = shift;
98 my $dbh = $this->{dbh};
99 if ( $this->{Active} && !$dbh->{AutoCommit} ) {
100 $dbh->commit || Carp::croak( $dbh->errstr );
101 }
102 $this->{Active} = 0;
103}
104
105sub rollback {
106 my $this = shift;
107 my $dbh = $this->{dbh};
108 if ( $this->{Active} && !$dbh->{AutoCommit} ) {
109 $dbh->rollback || Carp::croak( $dbh->errstr );
110 }
111 $this->{Active} = 0;
112}
113
114sub DESTROY {
115 my $this = shift;
116 $this->rollback;
117}
118
1191;