On 29 Jun 2001 09:00:50 -0700, Randal L. Schwartz wrote:
> >>>>> "Chas" == Chas Owens <[EMAIL PROTECTED]> writes:
>
> Chas> my $query = "
> Chas> SELECT a.filename, a.size, a.date, b.owner, c.group
> Chas> FROM filesystem a, outer owner b, outer group c
> Chas> WHERE a.uid = b.uid
> Chas> AND a.gid = b.gid";
>
> Chas> my @query_options = (
> Chas> " AND filename = ",
> Chas> " AND size = ",
> Chas> " AND date = "
> Chas> );
>
> Chas> my $i = 0;
>
> Chas> #thanks go to Merlyn for making me think about @ARGV instead of using
> Chas> #defined(ARGV[0])
>
> Chas> $query .= $query_options[$i++] . shift while (@ARGV);
>
> my @query_options = qw(filename size date);
Yay, I finally get to stand up for myself: I thought about using just
the column names, but if the option's operator should be OR or AND NOT
instead of just AND? Of course, you could always require an operator,
the field, an operator, and the data on the commandline like this:
find_files "AND filename = spec.txt" "AND size < 1024"
but this seems to be a utility script and that level of user interaction
is undesirable since the point of such script is often to just make
being a DBA easier. Of course, such a script would probably be much
nicer to use if we were to use Getopt::Std, but then all of these tricks
are pointless.
>
> $query .= sprintf " AND %s = %s", shift @query_options, shift while @ARGV;
>
> Except I'd probably go the placeholder approach...
>
> my @query_options = qw(filename size date);
> my $query = join " ", <<'END', map "AND $_ = ?", @query_options[0..$#ARGV];
> SELECT a.filename, a.size, a.date, b.owner, c.group
> FROM filesystem a, outer owner b, outer group c
> WHERE a.uid = b.uid AND a.gid = b.gid
> END
>
> my $sth = $dbh->prepare($query);
> $sth->execute($query, @ARGV);
> $sth->mumble_fetch_mumble...
>
> That way you don't have to worry about weird values in @ARGV.
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
> <[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
>
--
Today is Setting Orange, the 34th day of Confusion in the YOLD 3167
Hail Eris!