1 package Yaala::Data::Setup;
5 use vars qw#$USED_FIELDS $USED_AGGREGATIONS $SELECTS %DATAFIELDS#;
7 =head1 Yaala::Data::Setup
9 This module is currently under construction.
14 use Carp qw#carp cluck croak confess#;
15 use Yaala::Config qw#get_config#;
16 use Yaala::Data::Persistent qw#init#;
18 import Yaala::Parser qw#%DATAFIELDS#;
20 @Yaala::Data::Setup::ISA = ('Exporter');
21 @Yaala::Data::Setup::EXPORT_OK = qw#$USED_FIELDS $USED_AGGREGATIONS $SELECTS %DATAFIELDS#;
22 import Yaala::Parser qw#%DATAFIELDS#;
24 $USED_FIELDS = init ('$USED_FIELDS', 'hash');
25 $USED_AGGREGATIONS = init ('$USED_AGGREGATIONS', 'hash');
26 $SELECTS = init ('$SELECTS', 'array');
28 my $VERSION = '$Id: Setup.pm,v 1.14 2003/12/07 14:52:22 octo Exp $';
29 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
34 import Data::Dumper qw#Dumper#;
43 =head2 Yaala::Data::Setup::read_config
45 Parses the select-statements in the config file and returns configuration
46 data. To be called by Yaala::Data::Core.
51 print STDERR $/, __FILE__, ': ',
52 Data::Dumper->Dump ([\%DATAFIELDS], ['DATAFIELDS']) if ($::DEBUG & 0x20);
54 unless (get_config ('select'))
56 print STDERR $/, __FILE__, ": Please edit the config file first!\n";
60 for (get_config ('select'))
62 print STDERR $/, __FILE__, ": Select statement from config file: '$_'" if ($::DEBUG & 0x20);
63 my $select = parse_select ($_);
65 next unless (defined ($select));
67 push (@$SELECTS, $select);
69 $USED_AGGREGATIONS->{$_}++ for (@{$select->[0]});
70 $USED_FIELDS->{$_}++ for (@{$select->[1]});
71 $USED_FIELDS->{$_->[0]}++ for (@{$select->[2]});
73 print STDERR $/, __FILE__, ': New selection: ',
74 Data::Dumper->Dump ([$select], ['select']) if ($::DEBUG & 0x20);
77 if (!scalar (@$SELECTS))
79 print STDERR $/, __FILE__, ": No valid select-statements found. Exiting.\n";
84 # select: agg from fld1 [, fld2] [where fld3 = "value" ];
85 # select: bytes from date [, time] [where client = "leeloo.ff"];
93 if (grep { $line eq $_->[3] } (@$SELECTS))
95 print STDERR $/, __FILE__, ": Found duplicated selection ``$line''.",
96 $/, __FILE__, ": This is probably coming from Yaala::Data::Persistent and is nothing to worry about."
102 #if ($line =~ m/^(\w+) BY (\w+(?:,\s?\w+)*)(?: WHERE (.+))?$/i)
103 if ($line =~ m/^(\w+(?:\s*,\s*\w+)*)\s+BY\s+(\w+(?:\s*,\s*\w+)*)(?:\s+WHERE\s+(.+))?$/i)
105 my ($agg_exp, $fld_exp, $where_exp) = ($1, $2, $3);
108 for (split (m/\s*,\s*/, $agg_exp))
112 if (!defined ($DATAFIELDS{$agg}))
114 print STDERR $/, __FILE__, ": Aggregation ``$agg'' not provided by parser. ",
115 "Ignoring this aggregation.";
118 elsif ($DATAFIELDS{$agg} !~ m/^agg/i)
120 print STDERR $/, __FILE__, ": ``$agg'' is not an aggregation. Ignoring it.";
128 print STDERR $/, __FILE__, ": No valid aggregation found. Ignoring this select-statement.";
133 for (split (m/\s*,\s*/, $fld_exp))
137 if (!defined ($DATAFIELDS{$fld}))
139 print STDERR $/, __FILE__, ": Field '$fld' not provided by parser. Ignoring it.";
143 push (@fields, $fld);
145 if (!scalar (@fields))
147 print STDERR $/, __FILE__, ": No valid fields found. Ignoring this select-statement.";
151 my @wheres = parse_where ($where_exp);
153 $retval = [\@aggs, \@fields, \@wheres, $line];
157 print STDERR $/, __FILE__, ": Unable to parse select statement:",
158 $/, __FILE__, ": $line",
159 $/, __FILE__, ": Ignoring it.";
173 my $where_exp = shift;
176 if (!defined ($where_exp))
181 for (split (m/\s?,\s?/, $where_exp))
184 if ($exp =~ m/(\w+)\s?([<>=~!]+)\s?(.+)/)
186 my ($fld, $op, $val) = ($1, $2, $3);
187 if (!defined ($DATAFIELDS{$fld}))
189 print STDERR $/, __FILE__, ": Error in where-clause: Field '$fld' ",
190 "is unknown. Ignoring it.";
195 if ($DATAFIELDS{$fld} =~ m/:/)
197 $type = (split (m/:/, $DATAFIELDS{$fld}))[1];
200 unless ($op =~ m/^[<>=!]=$/
203 or $op eq '<' or $op eq '>')
205 print STDERR $/, __FILE__, ": Error in where-clause: Operator '$op' ",
206 "is unknown. Ignoring it.";
210 $val =~ s/^['"]|['"]$//g;
212 if ($type ne 'numeric')
214 $op = 'eq' if ($op eq '==');
215 $op = 'ne' if ($op eq '!=');
216 $op = 'gt' if ($op eq '>');
217 $op = 'ge' if ($op eq '>=');
218 $op = 'lt' if ($op eq '<');
219 $op = 'le' if ($op eq '<=');
221 elsif ($type eq 'numeric' and
222 ($op eq '=~' or $op eq '!~'))
224 print STDERR $/, __FILE__, ": Error in where clause: Can't use regex ",
225 "with numeric field $fld. Ignoring this clause.";
229 print STDERR $/, __FILE__, ": New where-statement: [$fld, $op, $val]" if ($::DEBUG & 0x20);
231 push (@where, [$fld, $op, $val]);
235 print STDERR $/, __FILE__, ": Error in where-clause: Unable to parse '$exp'. ",