[interchange-cvs] interchange - heins modified lib/Vend/Table/DBI.pm
interchange-core@interchange.redhat.com
interchange-core@interchange.redhat.com
Thu Oct 18 01:28:00 2001
User: heins
Date: 2001-10-18 05:27:41 GMT
Modified: lib/Vend/Table DBI.pm
Log:
* Add get_slice() method to db, analogous to set_slice, which I
meant to do a long time ago.
* This is sufficiently well-reasoned that it can go in stable.
Revision Changes Path
2.2 +39 -0 interchange/lib/Vend/Table/DBI.pm
rev 2.2, prev_rev 2.1
Index: DBI.pm
===================================================================
RCS file: /anon_cvs/repository/interchange/lib/Vend/Table/DBI.pm,v
retrieving revision 2.1
retrieving revision 2.2
diff -u -r2.1 -r2.2
--- DBI.pm 2001/10/18 04:16:16 2.1
+++ DBI.pm 2001/10/18 05:27:41 2.2
@@ -1,6 +1,6 @@
# Vend::Table::DBI - Access a table stored in an DBI/DBD database
#
-# $Id: DBI.pm,v 2.1 2001/10/18 04:16:16 mheins Exp $
+# $Id: DBI.pm,v 2.2 2001/10/18 05:27:41 mheins Exp $
#
# Copyright (C) 1996-2001 Red Hat, Inc. <interchange@redhat.com>
#
@@ -20,7 +20,7 @@
# MA 02111-1307 USA.
package Vend::Table::DBI;
-$VERSION = substr(q$Revision: 2.1 $, 10);
+$VERSION = substr(q$Revision: 2.2 $, 10);
use strict;
@@ -886,6 +886,45 @@
#::logDebug("cloned, key=$k");
}
return $new;
+}
+
+sub get_slice {
+ my ($s, $key, $fary) = @_;
+ $s = $s->import_db() if ! defined $s->[$DBI];
+
+ my $tkey;
+ my $sql;
+ return undef unless $s->record_exists($key);
+
+ $tkey = $s->quote($key, $s->[$KEY]);
+#::logDebug("tkey now $tkey");
+
+ # Better than failing on a bad ref...
+ if(ref $fary ne 'ARRAY') {
+ shift; shift;
+ $fary = [ @_ ];
+ }
+
+ my $fstring = join ",", @$fary;
+ $sql = "SELECT $fstring from $s->[$TABLE] WHERE $s->[$KEY] = $tkey";
+
+#::logDebug("get_slice query: $sql");
+#::logDebug("get_slice key/fields:\nkey=$key\n" . ::uneval($fary));
+ my $sth;
+ my $ary;
+ eval {
+ $sth = $s->[$DBI]->prepare($sql)
+ or die ::errmsg("prepare %s: %s", $sql, $DBI::errstr);
+ $sth->execute();
+ };
+
+ if($@) {
+ my $msg = $@;
+ ::logError("failed %s::%s routine: %s", __PACKAGE__, 'get_slice', $msg);
+ return undef;
+ }
+
+ return wantarray ? $sth->fetchrow_array() : $sth->fetchrow_arrayref();
}
sub set_slice {