[interchange-cvs] interchange - heins modified lib/Vend/Data.pm
interchange-cvs at icdevgroup.org
interchange-cvs at icdevgroup.org
Thu Feb 12 13:22:08 EST 2004
User: heins
Date: 2004-02-12 18:22:08 GMT
Modified: lib/Vend Data.pm
Log:
* Add ability to export only portions of tables based on a where= parameter.
Only works for DBI tables at the current time.
If the where parameter is a scalar, just passed as a "WHERE" clause,
i.e.
[export table=products where="prod_group='Ladders'"]
You can pass anything that won't cause a syntax error, even including
an "order by" or "limit".
If you want to pass multiple things, or not worry about quoting,
you can do:
[export table=products where.prod_group=Ladders]
The normal caveats for hash parameters apply, i.e. you cannot
do:
where.prod_group="[cgi foo]"
You *can* do:
where.prod_group=`$Tag->cgi('foo')`
or
where.prod_group=`$CGI->{foo}`
Normal DBI quoting is always done, so you don't include quotes.
Revision Changes Path
2.34 +22 -6 interchange/lib/Vend/Data.pm
rev 2.34, prev_rev 2.33
Index: Data.pm
===================================================================
RCS file: /var/cvs/interchange/lib/Vend/Data.pm,v
retrieving revision 2.33
retrieving revision 2.34
diff -u -r2.33 -r2.34
--- Data.pm 9 Dec 2003 11:05:41 -0000 2.33
+++ Data.pm 12 Feb 2004 18:22:08 -0000 2.34
@@ -1,6 +1,6 @@
# Vend::Data - Interchange databases
#
-# $Id: Data.pm,v 2.33 2003/12/09 11:05:41 racke Exp $
+# $Id: Data.pm,v 2.34 2004/02/12 18:22:08 mheins Exp $
#
# Copyright (C) 2002-2003 Interchange Development Group
# Copyright (C) 1996-2002 Red Hat, Inc.
@@ -1185,6 +1185,7 @@
$field = $opt->{field} if $opt->{field};
$delete = $opt->{delete} if $opt->{delete};
+
$db = database_exists_ref($db)
or do {
logError("Vend::Data export: non-existent database %s" , $db);
@@ -1193,6 +1194,21 @@
$db = $db->ref();
+ my $qual;
+ if($qual = $opt->{where}) {
+ if(ref $qual) {
+ my @out;
+ for(keys %$qual) {
+ my $val = $db->quote($qual->{$_}, $_);
+ push @out, "$_ = $val";
+ }
+ $qual = 'WHERE ' . join(" AND ", @out);
+ }
+ elsif($qual !~ /^\s*where\s+/i) {
+ $qual = "WHERE $qual";
+ }
+ }
+
my $table_name = $db->config('name');
my $notes;
if("\U$type" eq 'NOTES') {
@@ -1276,7 +1292,7 @@
print EXPORT '"';
print EXPORT join $delim, @cols;
print EXPORT qq%"\n%;
- while( (undef, @data) = $db->each_record() ) {
+ while( (undef, @data) = $db->each_record($qual) ) {
print EXPORT '"';
splice(@data, $nuke, 1) if defined $nuke;
$tempdata = join $delim, @data;
@@ -1308,7 +1324,7 @@
print EXPORT join "\n", @cols;
print EXPORT "\n$nf $sep\n\n";
my $i;
- while( (undef, @data) = $db->each_record() ) {
+ while( (undef, @data) = $db->each_record($qual) ) {
splice(@data, $nuke, 1) if defined $nuke;
my $nd = splice(@data, $nf_col, 1);
# Yes, we don't want the last field yet. 8-)
@@ -1326,7 +1342,7 @@
print EXPORT $record_delim;
my $detab = ($delim eq "\t") ? 1 : 0;
if(defined $nuke) {
- while( (undef, @data) = $db->each_record() ) {
+ while( (undef, @data) = $db->each_record($qual) ) {
splice(@data, $nuke, 1);
if ($detab) { s/\t/ /g for @data; }
$tempdata = join $delim, @data;
@@ -1335,7 +1351,7 @@
}
}
else {
- while( (undef, @data) = $db->each_record() ) {
+ while( (undef, @data) = $db->each_record($qual) ) {
if ($detab) { s/\t/ /g for @data; }
$tempdata = join $delim, @data;
$tempdata =~ s/\r?\n/\r/g;
@@ -1347,7 +1363,7 @@
print EXPORT join $delim, @cols;
print EXPORT $record_delim;
my $detab = ($delim eq "\t" or $record_delim eq "\t") ? 1 : 0;
- while( (undef, @data) = $db->each_record() ) {
+ while( (undef, @data) = $db->each_record($qual) ) {
splice(@data, $nuke, 1) if defined $nuke;
if ($detab) { s/\t/ /g for @data; }
print EXPORT join($delim, @data);
More information about the interchange-cvs
mailing list