* Don't autovifivy @fields array entries.
[interchange.git] / code / Filter / next_sequential.filter
1 # Copyright 2002-2007 Interchange Development Group and others
2 # Copyright 1996-2002 Red Hat, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.  See the LICENSE file for details.
8
9 # $Id: next_sequential.filter,v 1.6 2007-03-30 23:40:45 pajamian Exp $
10
11 CodeDef next_sequential Filter
12 CodeDef next_sequential Description Next sequential
13 CodeDef next_sequential Visibility  private
14 CodeDef next_sequential Routine <<EOR
15 sub {
16         my ($value, $field, $table, $col, $qualifier) = @_;
17 #::logDebug("called next_sequential filter value='$value' table=$table col=$col qual=$qualifier");
18         return $value if length($value);
19         $table ||= $CGI::values{mv_data_table};
20
21         my $val;
22
23         if(! $table) {
24                 return 1 if ! $field;
25                 return exists $CGI::values{$field}
26                                 ? ($CGI::values{$field})
27                                 : ($::Values->{$field});
28         }
29
30         $col ||= $field;
31         
32         eval {
33                 my $db = database_exists_ref($table)
34                         or die errmsg("next_sequential filter: no table '%s'", $table);
35                 my $tname = $db->name();
36                 my $q = "SELECT $col FROM $tname";
37                 if($qualifier) {
38                         my $qval = $CGI::values{$qualifier};
39                         $qval = $db->quote($qval, $qualifier);
40                         $q .= " WHERE $qualifier = $qval";
41                 }
42                 $q .= " ORDER BY $col desc";
43 #::logDebug("constructed query $q for next_sequential");
44                 my $ary = $db->query($q)
45                         or die errmsg("next_sequential filter query failed: %s", $q);
46                 return 1 unless @$ary;
47                 $val = $ary->[0][0];
48                 $val++;
49         };
50
51         if($@) {
52                 logError($@);
53                 return undef;
54         }
55
56         return $val;
57
58 }
59 EOR