3 # Library functions for the NewsStats package.
5 # Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
7 # This module can be redistributed and/or modified under the same terms under
8 # which Perl itself is published.
14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
47 %EXPORT_TAGS = ( TimePeriods => [qw(GetTimePeriod LastMonth SplitPeriod
49 Output => [qw(OutputData FormatOutput)],
50 SQLHelper => [qw(SQLHierarchies SQLSortOrder SQLGroupList
51 SQLSetBounds SQLBuildClause GetMaxLength)]);
53 our $PackageVersion = '0.01';
62 #####-------------------------------- Vars --------------------------------#####
64 # save $0 in $FullPath
66 # strip filename and /bin or /install directory to create the $HomePath
67 our $HomePath = dirname(realpath($0));
68 $HomePath =~ s/\/(bin|install)//;
72 our $MyVersion = "$0 $::VERSION (NewsStats.pm $VERSION)";
74 #####------------------------------- Basics -------------------------------#####
76 ################################################################################
78 ################################################################################
80 ################################################################################
81 ### display version and exit
82 print "NewsStats v$PackageVersion\n$MyVersion\n";
83 print "Copyright (c) 2010-2012 Thomas Hochstein <thh\@inter.net>\n";
84 print "This program is free software; you may redistribute it ".
85 "and/or modify it under the same terms as Perl itself.\n";
88 ################################################################################
90 ################################################################################
92 ################################################################################
93 ### feed myself to perldoc and exit
94 exec('perldoc', $FullPath);
97 ################################################################################
99 ################################################################################
101 ################################################################################
102 ### read config via Config::Auto
103 ### IN : $ConfFile: config filename
104 ### OUT: reference to a hash containing the configuration
107 $ConfFile = $HomePath . '/etc/newsstats.conf' if !$ConfFile;
108 # mandatory configuration options
109 my @Mandatory = ('DBDriver','DBHost','DBUser','DBPw','DBDatabase',
110 'DBTableRaw','DBTableGrps');
111 # read config via Config::Auto
112 my $ConfR = Config::Auto::parse($ConfFile, format => 'equal');
113 my %Conf = %{$ConfR};
114 # check for mandatory options
115 foreach (@Mandatory) {
116 &Bleat(2,sprintf("Mandatory configuration option %s is not set!",$_))
117 if (!defined($Conf{$_}));
119 # $Conf{'TLH'} is checked in gatherstats.pl
122 ################################################################################
124 ################################################################################
126 ################################################################################
127 ### override configuration values
128 ### IN : $ConfigR : reference to configuration hash
129 ### $OverrideR: reference to a hash containing overrides
130 my ($ConfigR,$OverrideR) = @_;
131 my %Override = %$OverrideR;
133 &Bleat(1,"Empty configuration hash passed to OverrideConfig()")
134 if ( keys %$ConfigR < 1);
135 # return if no overrides
136 return if (keys %Override < 1 or keys %$ConfigR < 1);
137 foreach my $Key (keys %Override) {
138 $$ConfigR{$Key} = $Override{$Key};
141 ################################################################################
143 ################################################################################
145 ################################################################################
146 ### initialise database connection
147 ### IN : $ConfigR: reference to configuration hash
148 ### $Die : if TRUE, die if connection fails
150 my ($ConfigR,$Die) = @_;
151 my %Conf = %$ConfigR;
152 my $DBHandle = DBI->connect(sprintf('DBI:%s:database=%s;host=%s',
153 $Conf{'DBDriver'},$Conf{'DBDatabase'},
154 $Conf{'DBHost'}), $Conf{'DBUser'},
155 $Conf{'DBPw'}, { PrintError => 0 });
157 &Bleat(2,$DBI::errstr) if (defined($Die) and $Die);
158 &Bleat(1,$DBI::errstr);
162 ################################################################################
164 ################################################################################
166 ################################################################################
167 ### print warning or error messages and terminate in case of error
168 ### IN : $Level : 1 = warning, 2 = error
169 ### $Message: warning or error message
170 my ($Level,$Message) = @_;
172 warn "$0 W: $Message\n"
173 } elsif ($Level == 2) {
174 die "$0 E: $Message\n"
176 print "$0: $Message\n"
179 ################################################################################
181 #####------------------------------ GetStats ------------------------------#####
183 ################################################################################
185 ################################################################################
186 ### explode a (scalar) list of newsgroup names to a list of newsgroup and
187 ### hierarchy names where every newsgroup and hierarchy appears only once:
188 ### de.alt.test,de.alt.admin -> de.ALL, de.alt.ALL, de.alt.test, de.alt.admin
189 ### IN : $Newsgroups : a list of newsgroups (content of Newsgroups: header)
190 ### $TLH : top level hierarchy (all other newsgroups are ignored)
191 ### $ValidGroupsR: reference to a hash containing all valid newsgroups
193 ### OUT: %Newsgroups : hash containing all newsgroup and hierarchy names as keys
194 my ($Newsgroups,$TLH,$ValidGroupsR) = @_;
195 my %ValidGroups = %{$ValidGroupsR} if $ValidGroupsR;
198 # remove whitespace from contents of Newsgroups:
199 $Newsgroups =~ s/\s//;
200 # call &HierarchyCount for each newsgroup in $Newsgroups:
201 for (split /,/, $Newsgroups) {
202 # don't count newsgroup/hierarchy in wrong TLH
203 next if($TLH and !/^$TLH/);
204 # don't count invalid newsgroups
205 if(%ValidGroups and !defined($ValidGroups{$_})) {
206 warn (sprintf("DROPPED: %s\n",$_));
209 # add original newsgroup to %Newsgroups
211 # add all hierarchy elements to %Newsgroups, amended by '.ALL',
212 # i.e. de.alt.ALL and de.ALL
213 foreach (ParseHierarchies($_)) {
214 $Newsgroups{$_.'.ALL'} = 1;
220 ################################################################################
221 sub ParseHierarchies {
222 ################################################################################
223 ### return a list of all hierarchy levels a newsgroup belongs to
224 ### (for de.alt.test.moderated that would be de/de.alt/de.alt.test)
225 ### IN : $Newsgroup : a newsgroup name
226 ### OUT: @Hierarchies: array containing all hierarchies the newsgroup belongs to
227 my ($Newsgroup) = @_;
229 # strip trailing dots
230 $Newsgroup =~ s/(.+)\.+$/$1/;
231 # butcher newsgroup name by "." and add each hierarchy to @Hierarchies
232 # i.e. de.alt.test: "de.alt" and "de"
233 while ($Newsgroup =~ /\./) {
234 $Newsgroup =~ s/^((?:\.?[^.]+)*)\.[^.]+$/$1/;
235 push @Hierarchies, $Newsgroup;
240 ################################################################################
242 ################################################################################
243 ### read a list of valid newsgroups from file (each group on one line,
244 ### ignoring everything after the first whitespace and so accepting files
245 ### in checkgroups format as well as (parts of) an INN active file)
246 ### IN : $Filename : file to read
247 ### OUT: \%ValidGroups: hash containing all valid newsgroups
250 open (my $LIST,"<$Filename") or &Bleat(2,"Cannot read $Filename: $!");
255 $ValidGroups{$_} = '1';
258 return \%ValidGroups;
261 ################################################################################
263 #####----------------------------- TimePeriods ----------------------------#####
265 ################################################################################
267 ################################################################################
268 ### get a time period to act on from --month option;
269 ### if empty, default to last month
270 ### IN : $Month: may be empty, 'YYYY-MM', 'YYYY-MM:YYYY-MM' or 'all'
271 ### OUT: $Verbal,$SQL: verbal description and WHERE-clause
272 ### of the chosen time period
274 # define result variables
276 # define a regular expression for a month
277 my $REMonth = '\d{4}-\d{2}';
279 # default to last month if option is not set
284 # check for valid input
285 if ($Month =~ /^$REMonth$/) {
286 # single month (YYYY-MM)
287 ($Month) = &CheckMonth($Month);
289 $SQL = sprintf("month = '%s'",$Month);
290 } elsif ($Month =~ /^$REMonth:$REMonth$/) {
291 # time period (YYYY-MM:YYYY-MM)
292 $Verbal = sprintf('%s to %s',&SplitPeriod($Month));
293 $SQL = sprintf("month BETWEEN '%s' AND '%s'",&SplitPeriod($Month));
294 } elsif ($Month =~ /^all$/i) {
296 $Verbal = 'all time';
300 return (undef,undef);
303 return ($Verbal,$SQL);
306 ################################################################################
308 ################################################################################
309 ### get last month from todays date in YYYY-MM format
310 ### OUT: last month as YYYY-MM
312 my (undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time);
313 # $Month is already defined from 0 to 11, so no need to decrease it by 1
320 return sprintf('%4d-%02d',$Year,$Month);
323 ################################################################################
325 ################################################################################
326 ### check if input (in YYYY-MM form) is valid with MM between 01 and 12;
327 ### otherwise, fix it
328 ### IN : @Month: array of month
329 ### OUT: @Month: a valid month
331 foreach my $Month (@Month) {
332 my ($OldMonth) = $Month;
333 my ($CalMonth) = substr ($Month, -2);
334 if ($CalMonth < 1 or $CalMonth > 12) {
335 $CalMonth = '12' if $CalMonth > 12;
336 $CalMonth = '01' if $CalMonth < 1;
337 substr($Month, -2) = $CalMonth;
338 &Bleat(1,sprintf("'%s' is an invalid date (MM must be between '01' ".
339 "and '12'), set to '%s'.",$OldMonth,$Month));
345 ################################################################################
347 ################################################################################
348 ### split a time period denoted by YYYY-MM:YYYY-MM into start and end month
349 ### IN : $Period: time period
350 ### OUT: $StartMonth, $EndMonth
352 my ($StartMonth, $EndMonth) = split /:/, $Period;
353 ($StartMonth,$EndMonth) = CheckMonth($StartMonth,$EndMonth);
354 # switch parameters as necessary
355 if ($EndMonth gt $StartMonth) {
356 return ($StartMonth, $EndMonth);
358 return ($EndMonth, $StartMonth);
362 ################################################################################
364 ################################################################################
365 ### return a list of months (YYYY-MM) between start and end month
366 ### IN : $MonthExpression ('YYYY-MM' or 'YYYY-MM to YYYY-MM')
367 ### OUT: @Months: array containing all months from $MonthExpression enumerated
368 my ($MonthExpression )= @_;
369 # return if single month
370 return ($MonthExpression) if ($MonthExpression =~ /^\d{4}-\d{2}$/);
371 # parse $MonthExpression
372 my ($StartMonth, $EndMonth) = split(' to ',$MonthExpression);
373 # set $Year, $Month from $StartMonth
374 my ($Year, $Month) = split /-/, $StartMonth;
377 until ("$Year-$Month" gt $EndMonth) {
378 push @Months, "$Year-$Month";
379 $Month = "$Month"; # force string context
389 #####---------------------------- OutputFormats ---------------------------#####
391 ################################################################################
393 ################################################################################
394 ### read database query results from DBHandle and print results with formatting
395 ### IN : $Format : format specifier
396 ### $Comments : print or suppress all comments for machine-readable output
397 ### $GroupBy : primary sorting order (month or key)
398 ### $Precision: number of digits right of decimal point (0 or 2)
399 ### $ValidKeys: reference to a hash containing all valid keys
400 ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
401 ### $DBQuery : database query handle with executed query,
402 ### containing $Month, $Key, $Value
403 ### $PadField : padding length for key field (optional) for 'pretty'
404 ### $PadValue : padding length for value field (optional) for 'pretty'
405 my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl,
406 $DBQuery, $PadField, $PadValue) = @_;
407 my %ValidKeys = %{$ValidKeys} if $ValidKeys;
408 my ($FileName, $Handle, $OUT);
411 # define output types
413 @LegalOutput{('dump','list','pretty')} = ();
414 # bail out if format is unknown
415 &Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format});
417 while (my ($Month, $Key, $Value) = $DBQuery->fetchrow_array) {
418 # don't display invalid keys
419 if(%ValidKeys and !defined($ValidKeys{$Key})) {
421 # &Bleat(1,sprintf("DROPPED: %s",$Key));
424 # care for correct sorting order and abstract from month and keys:
425 # $Caption will be $Month or $Key, according to sorting order,
426 # and $Key will be $Key or $Month, respectively
428 if ($GroupBy eq 'key') {
434 # set output file handle
436 $Handle = *STDOUT{IO}; # set $Handle to a reference to STDOUT
437 } elsif (!defined($LastIteration) or $LastIteration ne $Caption) {
438 close $OUT if ($LastIteration);
439 # safeguards for filename creation:
440 # replace potential problem characters with '_'
441 $FileName = sprintf('%s-%s',$FileTempl,$Caption);
442 $FileName =~ s/[^a-zA-Z0-9_-]+/_/g;
443 open ($OUT,">$FileName")
444 or &Bleat(2,sprintf("Cannot open output file '%s': $!",
448 print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value,
449 $Precision, $PadField, $PadValue);
450 $LastIteration = $Caption;
452 close $OUT if ($FileTempl);
455 ################################################################################
457 ################################################################################
458 ### format information for output according to format specifier
459 ### IN : $Format : format specifier
460 ### $Comments : print or suppress all comments for machine-readable output
461 ### $Caption : month (as YYYY-MM) or $Key, according to sorting order
462 ### $Key : newsgroup, client, ... or $Month, as above
463 ### $Value : number of postings with that attribute
464 ### $Precision: number of digits right of decimal point (0 or 2)
465 ### $PadField : padding length for key field (optional) for 'pretty'
466 ### $PadValue : padding length for value field (optional) for 'pretty'
467 ### OUT: $Output: formatted output
468 my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadField,
471 # keep last caption in mind
472 our ($LastIteration);
473 # create one line of output
474 if ($Format eq 'dump') {
475 # output as dump (key value)
476 $Output = sprintf ("# %s:\n",$Caption)
477 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
478 $Output .= sprintf ("%s %u\n",$Key,$Value);
479 } elsif ($Format eq 'list') {
480 # output as list (caption key value)
481 $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value);
482 } elsif ($Format eq 'pretty') {
484 $Output = sprintf ("# ----- %s:\n",$Caption)
485 if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
486 # increase $PadValue for numbers with decimal point
487 $PadValue += $Precision+1 if $Precision;
488 # add padding if $PadField is set; $PadValue HAS to be set then
489 $Output .= sprintf ($PadField ?
490 sprintf("%%-%us%%s %%%u.*f\n",$PadField,$PadValue) :
491 "%s%s %.*f\n",$Key,$Comments ? ':' : '',
497 #####------------------------- QueryModifications -------------------------#####
499 ################################################################################
501 ################################################################################
502 ### add exclusion of hierarchy levels (de.alt.ALL) from SQL query by
503 ### amending the WHERE clause if $ShowHierarchies is false (or don't, if it is
504 ### true, accordingly)
505 ### IN : $ShowHierarchies: boolean value
507 my ($ShowHierarchies) = @_;
508 return $ShowHierarchies ? '' : "newsgroup NOT LIKE '%.ALL'";
511 ################################################################################
513 ################################################################################
514 ### get length of longest fields in future query result
515 ### IN : $DBHandle : database handle
516 ### $Table : table to query
517 ### $Field : field (key!, i.e. month, newsgroup, ...) to check
518 ### $Value : field (value!, i.e. postings) to check
519 ### $WhereClause : WHERE clause
520 ### $HavingClause: HAVING clause
521 ### @BindVars : bind variables for WHERE clause
522 ### OUT: $FieldLength : length of longest instance of $Field
523 ### $ValueLength : length of longest instance of $Value
524 my ($DBHandle,$Table,$Field,$Value,$WhereClause,$HavingClause,@BindVars) = @_;
525 my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)),".
527 "FROM %s %s %s",$Field,,$Value,
528 $Table,$WhereClause,$HavingClause ?
529 'GROUP BY newsgroup' . $HavingClause .
530 ' ORDER BY LENGTH(newsgroup) '.
531 'DESC LIMIT 1': ''));
532 $DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ".
533 "for '%s' from table '%s': ".
534 "$DBI::errstr",$Field,$Table));
535 my ($FieldLength,$ValueMax) = $DBQuery->fetchrow_array;
536 my $ValueLength = length($ValueMax) if ($ValueMax);
537 return ($FieldLength,$ValueLength);
540 ################################################################################
542 ################################################################################
543 ### build a SQL 'ORDER BY' clause from $OptGroupBy (primary sorting) and
544 ### $OptOrderBy (secondary sorting), both ascending or descending;
545 ### descending sorting order is done by adding '-desc'
546 ### IN : $GroupBy: primary sort by 'month' (default) or 'newsgroups'
547 ### $OrderBy: secondary sort by month/newsgroups (default)
548 ### or number of 'postings'
549 ### OUT: a SQL ORDER BY clause
550 my ($GroupBy,$OrderBy) = @_;
551 my ($GroupSort,$OrderSort) = ('','');
552 # $GroupBy (primary sorting)
556 ($GroupBy, $GroupSort) = SQLParseOrder($GroupBy);
557 if ($GroupBy =~ /group/i) {
558 $GroupBy = 'newsgroup';
563 my $Secondary = ($GroupBy eq 'month') ? 'newsgroup' : 'month';
564 # $OrderBy (secondary sorting)
566 $OrderBy = $Secondary;
568 ($OrderBy, $OrderSort) = SQLParseOrder($OrderBy);
569 if ($OrderBy =~ /posting/i) {
570 $OrderBy = "postings $OrderSort, $Secondary";
572 $OrderBy = "$Secondary $OrderSort";
575 return ($GroupBy,&SQLBuildClause('order',"$GroupBy $GroupSort",$OrderBy));
578 ################################################################################
580 ################################################################################
581 ### parse $OptGroupBy or $OptOrderBy option of the form param[-desc], e.g.
582 ### 'month', 'month-desc', 'newsgroups-desc', but also just 'desc'
583 ### IN : $OrderOption: order option (see above)
584 ### OUT: parameter to sort by,
585 ### sort order ('DESC' or nothing, meaning 'ASC')
586 my ($OrderOption) = @_;
588 if ($OrderOption =~ s/-?desc$//i) {
591 $OrderOption =~ s/-?asc$//i
593 return ($OrderOption,$SortOrder);
596 ################################################################################
598 ################################################################################
599 ### explode list of newsgroups separated by : (with wildcards)
600 ### to a SQL 'WHERE' expression
601 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
602 ### OUT: SQL code to become part of a 'WHERE' clause,
603 ### list of newsgroups for SQL bindings
604 my ($Newsgroups) = @_;
605 # substitute '*' wildcard with SQL wildcard character '%'
606 $Newsgroups =~ s/\*/%/g;
607 return (undef,undef) if !CheckValidNewsgroups($Newsgroups);
608 # just one newsgroup?
609 return (SQLGroupWildcard($Newsgroups),$Newsgroups) if $Newsgroups !~ /:/;
610 my ($SQL,@WildcardGroups,@NoWildcardGroups);
611 # list of newsgroups separated by ':'
612 my @GroupList = split /:/, $Newsgroups;
613 foreach (@GroupList) {
615 # add to list of newsgroup names WITHOUT wildcard
616 push (@NoWildcardGroups,$_);
618 # add to list of newsgroup names WITH wildcard
619 push (@WildcardGroups,$_);
620 # add wildcard to SQL clause
621 # 'OR' if SQL clause is not empty
622 $SQL .= ' OR ' if $SQL;
623 $SQL .= 'newsgroup LIKE ?'
626 if (scalar(@NoWildcardGroups)) {
627 # add 'OR' if SQL clause is not empty
628 $SQL .= ' OR ' if $SQL;
629 if (scalar(@NoWildcardGroups) < 2) {
630 # special case: just one newsgroup without wildcard
631 $SQL .= 'newsgroup = ?';
633 # create list of newsgroups to include: 'newsgroup IN (...)'
634 $SQL .= 'newsgroup IN (';
636 foreach (@NoWildcardGroups) {
637 $SQLin .= ',' if $SQLin;
640 # add list to SQL clause
641 $SQL .= $SQLin .= ')';
644 # add brackets '()' to SQL clause as needed (more than one wildcard group)
645 if (scalar(@WildcardGroups)) {
646 $SQL = '(' . $SQL .')';
648 # rebuild @GroupList in (now) correct order
649 @GroupList = (@WildcardGroups,@NoWildcardGroups);
650 return ($SQL,@GroupList);
653 ################################################################################
654 sub SQLGroupWildcard {
655 ################################################################################
656 ### build a valid SQL 'WHERE' expression with or without wildcards
657 ### IN : $Newsgroup: newsgroup expression, probably with wildcard
658 ### (group.name or group.name.%)
659 ### OUT: SQL code to become part of a 'WHERE' clause
660 my ($Newsgroup) = @_;
661 if ($Newsgroup !~ /%/) {
662 return 'newsgroup = ?';
664 return 'newsgroup LIKE ?';
668 ################################################################################
670 ################################################################################
671 ### set upper and/or lower boundary (number of postings)
672 ### IN : $Type: 'level', 'average', 'sum' or 'default'
673 ### $LowBound,$UppBound: lower/upper boundary, respectively
674 ### OUT: SQL code to become part of a WHERE or HAVING clause
675 my ($Type,$LowBound,$UppBound) = @_;
676 ($LowBound,$UppBound) = SQLCheckNumber($LowBound,$UppBound);
677 if($LowBound and $UppBound and $LowBound > $UppBound) {
678 &Bleat(1,"Lower boundary $LowBound is larger than Upper boundary ".
679 "$UppBound, exchanging boundaries.");
680 ($LowBound,$UppBound) = ($UppBound,$LowBound);
682 # default to 'default'
683 my $WhereHavingFunction = 'postings';
684 # set $LowBound to SQL statement:
685 # 'WHERE postings >=', 'HAVING MIN(postings) >=' or 'HAVING AVG(postings) >='
686 if ($Type eq 'level') {
687 $WhereHavingFunction = 'MIN(postings)'
688 } elsif ($Type eq 'average') {
689 $WhereHavingFunction = 'AVG(postings)'
690 } elsif ($Type eq 'sum') {
691 $WhereHavingFunction = 'SUM(postings)'
693 $LowBound = sprintf('%s >= '.$LowBound,$WhereHavingFunction) if ($LowBound);
694 # set $LowBound to SQL statement:
695 # 'WHERE postings <=', 'HAVING MAX(postings) <=' or 'HAVING AVG(postings) <='
696 if ($Type eq 'level') {
697 $WhereHavingFunction = 'MAX(postings)'
698 } elsif ($Type eq 'average') {
699 $WhereHavingFunction = 'AVG(postings)'
700 } elsif ($Type eq 'sum') {
701 $WhereHavingFunction = 'SUM(postings)'
703 $UppBound = sprintf('%s <= '.$UppBound,$WhereHavingFunction) if ($UppBound);
704 return ($LowBound,$UppBound);
707 ################################################################################
709 ################################################################################
710 ### check if input is a valid positive integer; otherwise, make it one
711 ### IN : @Numbers: array of parameters
712 ### OUT: @Numbers: a valid positive integer
714 foreach my $Number (@Numbers) {
715 if ($Number and $Number < 0) {
716 &Bleat(1,"Boundary $Number is < 0, set to ".-$Number);
719 $Number = '' if ($Number and $Number !~ /^\d+$/);
724 ################################################################################
726 ################################################################################
727 ### build a valid SQL WHERE, GROUP BY, ORDER BY or HAVING clause
728 ### from multiple expressions which *may* be empty
729 ### IN : $Type: 'where', 'having', 'group' or 'order'
730 ### @Expressions: array of expressions
731 ### OUT: $SQLClause: a SQL clause
732 my ($Type,@Expressions) = @_;
733 my ($SQLClause,$Separator,$Statement);
734 # set separator ('AND' or ',')
735 if ($Type eq 'where' or $Type eq 'having') {
741 if ($Type eq 'where') {
742 $Statement = 'WHERE';
743 } elsif ($Type eq 'order') {
744 $Statement = 'ORDER BY';
745 } elsif ($Type eq 'having') {
746 $Statement = 'HAVING';
748 $Statement = 'GROUP BY';
750 # build query from expressions with separators
751 foreach my $Expression (@Expressions) {
753 $SQLClause .= " $Separator " if ($SQLClause);
754 $SQLClause .= $Expression;
757 # add statement in front if not already present
758 $SQLClause = " $Statement " . $SQLClause
759 if ($SQLClause and $SQLClause !~ /$Statement/);
763 #####--------------------------- Verifications ----------------------------#####
765 ################################################################################
766 sub CheckValidNewsgroups {
767 ################################################################################
768 ### syntax check of newgroup list
769 ### IN : $Newsgroups: list of newsgroups (group.one.*:group.two:group.three.*)
771 my ($Newsgroups) = @_;
772 my $InvalidCharRegExp = ',; ';
773 return ($Newsgroups =~ /[$InvalidCharRegExp]/) ? 0 : 1;
777 #####------------------------------- done ---------------------------------#####