| 1293 |
dpurdie |
1 |
###############################################################################
|
|
|
2 |
# Codestriker: Copyright (c) 2001, 2002 David Sitsky. All rights reserved.
|
|
|
3 |
# sits@users.sourceforge.net
|
|
|
4 |
#
|
|
|
5 |
# This program is free software; you can redistribute it and modify it under
|
|
|
6 |
# the terms of the GPL.
|
|
|
7 |
|
|
|
8 |
# Simple object used for retrieving and keeping a record of active DBI
|
|
|
9 |
# database connections.
|
|
|
10 |
|
|
|
11 |
package Codestriker::DB::DBI;
|
|
|
12 |
|
|
|
13 |
use strict;
|
|
|
14 |
use Codestriker;
|
|
|
15 |
use Codestriker::DB::Database;
|
|
|
16 |
|
|
|
17 |
# DBI connections are expensive to make, only have one per process, and when
|
|
|
18 |
# the code asks for a connection, just keep returning the same one.
|
|
|
19 |
our $connection;
|
|
|
20 |
|
|
|
21 |
# Retrieve a connection to the codestriker database for the specified
|
|
|
22 |
sub get_connection($) {
|
|
|
23 |
my ($type) = @_;
|
|
|
24 |
|
|
|
25 |
# Making a connection is expensive, cache it.
|
|
|
26 |
if ( !defined($connection) ) {
|
|
|
27 |
my $database = Codestriker::DB::Database->get_database();
|
|
|
28 |
|
|
|
29 |
$connection = $database->get_connection();
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
return $connection;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
# Release a connection, and if $success is true and this is a transaction
|
|
|
36 |
# controlled database, commit the transaction, otherwise abort it.
|
|
|
37 |
sub release_connection($$$) {
|
|
|
38 |
my ($type, $connection, $success) = @_;
|
|
|
39 |
|
|
|
40 |
# If the connection is transaction controlled, commit or abort the
|
|
|
41 |
# transaction depending on the value of $success.
|
|
|
42 |
if ($connection->{AutoCommit} == 0) {
|
|
|
43 |
$success ? $connection->commit : $connection->rollback;
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
1;
|