/usr/share/perl5/Octopussy/DB.pm is in octopussy 1.0.6-0ubuntu2.
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 | # $HeadURL$
# $Revision: 353 $
# $Date: 2010-05-17 18:44:55 +0100 (Mon, 17 May 2010) $
# $Author: sebthebert $
=head1 NAME
Octopussy::DB - Octopussy Database module
=cut
package Octopussy::DB;
use strict;
use warnings;
use DBI;
use AAT::DB;
use AAT::Syslog;
use Octopussy::Plugin;
use Octopussy::Table;
my @sql_substitutions = (
{
regexp => '^(COUNT\(DISTINCT\((.+?)\)\))',
substitution => 'COUNT_DISTINCT_',
value => 'COUNT_DISTINCT_'
},
{
regexp => '^(COUNT\((.+?)\))',
substitution => 'COUNT_',
value => 'COUNT_'
},
{
regexp => '^(SUM\((.+?)\))',
substitution => 'SUM_',
value => 'SUM_'
},
{
regexp => '^(AVG\((.+)?\))',
substitution => 'AVG_',
value => 'AVG_'
},
{
regexp => '^(MIN\((.+)?\))',
substitution => 'MIN_',
value => 'MIN_'
},
{
regexp => '^(MAX\((.+)?\))',
substitution => 'MAX_',
value => 'MAX_'
},
{
regexp => '^(DAY\((.+?)\))',
substitution => '\'%d/%m/%Y\') as D_',
value => 'D_'
},
{
regexp => '^(DAY_HOUR\((.+?)\))',
substitution => '\'%d/%m/%Y %Hh\') as DH_',
value => 'DH_'
},
{
regexp => '^(DAY_HOUR_MIN\((.+?)\))',
substitution => '\'%d/%m/%Y %Hh%i\') as DHM_',
value => 'DHM_'
},
{
regexp => '^(UNIX_TIMESTAMP\((.+?)\))',
substitution => '\'%Y-%m-%d %H:%i:00\')) as UT_',
value => 'UT_'
},
);
=head1 FUNCTIONS
=head2 Connect()
Connects to the Octopussy Database
=cut
sub Connect
{
my $error = AAT::DB::Connect('Octopussy');
AAT::Syslog::Message('Octopussy_DB', $error) if (defined $error);
return ($error);
}
=head2 Table_Creation($tablename, \@fields, \@indexes)
Creates Table '$tablename' with fields '\@fields'
=cut
sub Table_Creation
{
my ($tablename, $fields, $indexes) = @_;
my $sql = Octopussy::Table::SQL($tablename, $fields, $indexes);
AAT::DB::Do('Octopussy', $sql);
return ($sql);
}
=head2 SQL_As_Substitution($field)
=cut
sub SQL_As_Substitution
{
my $field = shift;
foreach my $s (@sql_substitutions)
{
if ($field =~ /^(\S+::\S+?)\((\S+)\)/) { $field = $2; }
elsif ($field =~ /$s->{regexp}/) { $field = "$s->{value}$2"; }
}
return ($field);
}
=head2 SQL_Select_Function(@fields)
=cut
sub SQL_Select_Function
{
my @fields = @_;
my @new_fields = ();
my $query = 'SELECT ';
foreach my $field (@fields)
{
my ($func, $func_field) = (undef, undef);
if ($field =~ /^(\S+::\S+?)\((\S+)\)/)
{
($func, $func_field) = ($1, $2);
if (Octopussy::Plugin::Function_Source($func) eq 'INPUT')
{
my $func_bis = $func;
$func_bis =~ s/^Octopussy:://;
$func_bis =~ s/::/_/g;
$field = $func_bis . "__$func_field";
}
else
{
$field = $2;
}
}
my $match = 0;
foreach my $s (@sql_substitutions)
{
if ($field =~ /$s->{regexp}/)
{
my ($fct, $param) = ($1, $2);
$field = (
(($field !~ /^DAY/) && ($field !~ /^UNIX/))
? "$fct as $s->{substitution}$param"
: (
($field =~ /^UNIX/)
? "UNIX_TIMESTAMP(DATE_FORMAT($param,$s->{substitution}$param"
: "DATE_FORMAT($param,$s->{substitution}$param"
)
);
my $cfield =
(defined $func ? "$func($s->{value}$param)" : "$s->{value}$param");
push @new_fields, $cfield;
$match = 1;
last;
}
}
my $complete_field = (defined $func ? "$func($func_field)" : $field);
push @new_fields, $complete_field if (!$match);
$query .= "$field, ";
}
$query =~ s/, $/ /;
return ($query, \@new_fields);
}
=head2 Column_Names($query)
Returns list of Column names from an SQL query
=cut
sub Column_Names
{
my $query = shift;
my %hash_columns = ();
my @columns = ();
if ($query =~ /SELECT (.+) FROM/i)
{
my @data = split /, /, $1;
foreach my $f (@data)
{
$f =~ s/\S+ AS (.+)$/$1/i;
$f =~ s/^\s*\(?COUNT\(//gi;
$f =~ s/^\s*\(?DISTINCT\(//gi;
$f =~ s/^\s*\(?SUM\(//gi;
$f =~ s/^\s*\(?AVG\(//gi;
$f =~ s/^\s*\(?UNIX_TIMESTAMP\(//gi;
$f =~ s/^\s*DATE_FORMAT\((\S+),\'(.+)?\'\)/$1/gi;
$f =~ s/[\(,\)]//gi;
$f =~ s/^\s*//i;
$f =~ s/\s*$//i;
$hash_columns{$f} = 1;
}
}
foreach my $k (keys %hash_columns) { push @columns, $k; }
return (@columns);
}
1;
=head1 AUTHOR
Sebastien Thebert <octo.devel@gmail.com>
=cut
|