diff options
author | Adam Frisby | 2008-03-25 16:29:54 +0000 |
---|---|---|
committer | Adam Frisby | 2008-03-25 16:29:54 +0000 |
commit | 8c05b81d6860d8e0da4098cc6f59f630d7c53037 (patch) | |
tree | 3325e09660ee8ad5a12cc19c8da6c4bd8bff5c21 /share/perl/lib/DBHandler.pm | |
parent | * On Ray casting. Now returning the face normal of the collision face. (diff) | |
download | opensim-SC_OLD-8c05b81d6860d8e0da4098cc6f59f630d7c53037.zip opensim-SC_OLD-8c05b81d6860d8e0da4098cc6f59f630d7c53037.tar.gz opensim-SC_OLD-8c05b81d6860d8e0da4098cc6f59f630d7c53037.tar.bz2 opensim-SC_OLD-8c05b81d6860d8e0da4098cc6f59f630d7c53037.tar.xz |
* Committing Lulurun's Cgi/Perl implementation of the UGAI servers.
* I love you long time.
Diffstat (limited to '')
-rw-r--r-- | share/perl/lib/DBHandler.pm | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/share/perl/lib/DBHandler.pm b/share/perl/lib/DBHandler.pm new file mode 100644 index 0000000..1435ba2 --- /dev/null +++ b/share/perl/lib/DBHandler.pm | |||
@@ -0,0 +1,119 @@ | |||
1 | use strict; | ||
2 | use DBI; | ||
3 | use Carp; | ||
4 | |||
5 | package DBHandler; | ||
6 | |||
7 | #our $dbh = undef; | ||
8 | use vars qw ($DB_CONNECTION); | ||
9 | |||
10 | sub 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 | ||
21 | package Statement; | ||
22 | |||
23 | sub 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 | |||
36 | sub 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 | |||
59 | sub last_id { | ||
60 | my $this = shift; | ||
61 | my $dbh = $this->{dbh}; | ||
62 | return $dbh->last_insert_id(undef, undef, undef, undef); | ||
63 | } | ||
64 | |||
65 | sub DESTROY { | ||
66 | my $this = shift; | ||
67 | my $sth = $this->{sth}; | ||
68 | $sth->finish(); | ||
69 | } | ||
70 | |||
71 | # ############# | ||
72 | # Transaction | ||
73 | package Transaction; | ||
74 | |||
75 | my $IS_TRANS = 1; | ||
76 | |||
77 | sub 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 | |||
88 | sub 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 | |||
96 | sub 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 | |||
105 | sub 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 | |||
114 | sub DESTROY { | ||
115 | my $this = shift; | ||
116 | $this->rollback; | ||
117 | } | ||
118 | |||
119 | 1; | ||