next_sequential
Look up a numeric value,
from a named column
in a named table,
and return the value plus 1.
|
Note
If the filter's body text has a true value
(not blank and not zero),
then that value will be returned verbatim.
|
|
Warning
This filter does not save the new sequence number back to the table.
|
Example
[value name="foo" set="bar" hide=1]
[filter next_sequential.mytable.mycol.foo][/filter]
|
Generates and executes SQL like the following:
SELECT mycol
FROM mytable
WHERE foo = 'bar'
|
The filter's return value will be whatever "mycol" contained,
plus 1.
Source code
sub {
my ($value, $field, $table, $col, $qualifier) = @_;
return $value if length($value);
$table ||= $CGI::values{mv_data_table};
my $val;
if(! $table) {
return 1 if ! $field;
return exists $CGI::values{$field}
? ($CGI::values{$field})
: ($::Values->{$field});
}
$col ||= $field;
eval {
my $db = database_exists_ref($table)
or die errmsg("next_sequential filter: no table '%s'", $table);
my $tname = $db->name();
my $q = "SELECT $col FROM $tname";
if($qualifier) {
my $qval = $CGI::values{$qualifier};
$qval = $db->quote($qval, $qualifier);
$q .= " WHERE $qualifier = $qval";
}
$q .= " ORDER BY $col desc";
my $ary = $db->query($q)
or die errmsg("next_sequential filter query failed: %s", $q);
return 1 unless @$ary;
$val = $ary->[0][0];
$val++;
};
if($@) {
logError($@);
return undef;
}
return $val;
}
|