/usr/share/perl5/AAT/DB.pm is in octopussy 1.0.6-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | # $HeadURL$
# $Revision: 353 $
# $Date: 2010-05-17 18:44:55 +0100 (Mon, 17 May 2010) $
# $Author: sebthebert $
=head1 NAME
AAT::DB - AAT Database module
=cut
package AAT::DB;
use strict;
use warnings;
use DBI;
use AAT::Application;
use AAT::Syslog;
use AAT::Utils qw( ARRAY HASH_KEYS );
use AAT::XML;
my %conf_file = ();
my %dbh = ();
=head1 FUNCTIONS
=head2 Configuration($appli)
Returns the Database configuration for the application '$appli'
=cut
sub Configuration
{
my $appli = shift;
$conf_file{$appli} ||= AAT::Application::File($appli, 'db');
my $conf = AAT::XML::Read($conf_file{$appli}, 1);
return ($conf->{database});
}
=head2 Connect($appli)
Connects to the Database for the application '$appli'
=cut
sub Connect
{
my $appli = shift;
my $conf_db = Configuration($appli);
my $type = $conf_db->{type} || 'mysql';
$dbh{$appli} =
DBI->connect("DBI:$type:database=$conf_db->{db};host=$conf_db->{host}",
$conf_db->{user}, $conf_db->{password});
return ("$DBI::err: $DBI::errstr") if (!defined $dbh{$appli});
return (undef);
}
=head2 Connection_Test($appli)
Checks the Database Connection for the application '$appli'
=cut
sub Connection_Test
{
my $appli = shift;
Connect($appli);
my $status = (defined $dbh{$appli} ? 1 : 0);
Disconnect($appli);
return ($status);
}
=head2 Disconnect($appli)
Disconnects from Database application '$appli'
=cut
sub Disconnect
{
my $appli = shift;
$dbh{$appli}->disconnect() if (defined $dbh{$appli});
$dbh{$appli} = undef;
return (undef);
}
=head2 Do($appli, $sql)
Does the SQL action '$sql' in application '$appli'
=cut
sub Do
{
my ($appli, $sql) = @_;
Connect($appli);
$dbh{$appli}->do($sql) if (defined $dbh{$appli});
Disconnect($appli);
return (1);
}
=head2 Table_Destruction($appli, $table)
Drops the Table '$table' in application '$appli'
=cut
sub Table_Destruction
{
my ($appli, $table) = @_;
Do($appli, "DROP TABLE IF EXISTS $table");
return (1);
}
=head2 Insert($appli, $table, $field_values)
Inserts values '$field_values' in Table '$table' in application '$appli'
=cut
sub Insert
{
my ($appli, $table, $field_values) = @_;
Connect($appli);
if (defined $dbh{$appli})
{
my $sql = "INSERT INTO $table(";
$sql .= join ', ', sort(HASH_KEYS($field_values));
$sql .= ') VALUES(';
foreach my $k (sort(HASH_KEYS($field_values)))
{
$sql .= $dbh{$appli}->quote($field_values->{$k}) . ', ';
}
$sql =~ s/, $/\)/;
$dbh{$appli}->do($sql);
Disconnect($appli);
}
return (1);
}
=head2 Prepare($appli, $sql)
Prepares the SQL statement '$sql' in application '$appli'
=cut
sub Prepare
{
my ($appli, $sql) = @_;
Connect($appli);
my $prepared = ((defined $dbh{$appli}) ? $dbh{$appli}->prepare($sql) : undef);
Disconnect($appli);
return ($prepared);
}
=head2 Query($appli, $query)
Executes the SQL Query '$query' in application '$appli'
=cut
sub Query
{
my ($appli, $query) = @_;
Connect($appli);
if (defined $dbh{$appli})
{
my $sth = $dbh{$appli}->prepare($query);
$sth->execute();
my @data = ();
while (my $ref = $sth->fetchrow_hashref()) { push @data, $ref; }
Disconnect($appli);
return (@data);
}
return (undef);
}
=head2 Load_File($appli, $table, $file, $lines)
Loads Data File '$file' with lines '$lines'
into table '$table' in application '$appli'
=cut
sub Load_Infile
{
my ($appli, $table, $file, $lines) = @_;
my $conf = Configuration($appli);
if (defined open my $DBFILE, '>', $file)
{
foreach my $l (ARRAY($lines))
{
print {$DBFILE} "$l\n"
if ($l =~ /\S+/);
}
close $DBFILE;
if ($conf->{type} eq 'mysql')
{
Do($appli, "LOAD DATA INFILE '$file' INTO TABLE $table" . "_$$");
}
elsif ($conf->{type} eq 'Pg')
{
Do($appli, "COPY ${table}_${$} FROM '$file'");
}
}
else
{
my ($pack, $file, $line, $sub) = caller 0;
AAT::Syslog::Message('AAT_DB', "Unable to open file '$file' in $sub");
}
return (1);
}
1;
=head1 SEE ALSO
AAT(3), AAT::Syslog(3), AAT::Theme(3), AAT::Translation(3), AAT::User(3), AAT::XML(3)
=head1 AUTHOR
Sebastien Thebert <octo.devel@gmail.com>
=cut
|