"Records identical" carp tells us it is just a warning.
[freeside.git] / site_perl / Record.pm
index 9b30850..b8d565c 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use vars qw($dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK);
 use subs qw(reload_dbdef);
 use Exporter;
-use Carp;
+use Carp qw(carp cluck croak confess);
 use File::CounterFile;
 use FS::UID qw(dbh checkruid swapuid getotaker datasrc);
 use FS::dbdef;
@@ -12,11 +12,12 @@ use FS::dbdef;
 @ISA = qw(Exporter);
 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef);
 
-$File::CounterFile::DEFAULT_DIR = "/var/spool/freeside/counters" ;
-
-$dbdef_file = "/var/spool/freeside/dbdef.". datasrc;
-
-reload_dbdef unless $setup_hack;
+#ask FS::UID to run this stuff for us later
+$FS::UID::callback{'FS::Record'} = sub { 
+  $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
+  $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
+  &reload_dbdef unless $setup_hack; #$setup_hack needed now?
+};
 
 =head1 NAME
 
@@ -127,7 +128,7 @@ sub new {
   #  }
   #}
 
-  foreach my $column (keys %{$hashref}) {
+  foreach my $column ( FS::Record::fields $table ) { 
     #trim the '$' from money fields for Pg (beong HERE?)
     #(what about Pg i18n?)
     if ( datasrc =~ m/Pg/ 
@@ -151,7 +152,9 @@ sub new {
 =item qsearch TABLE, HASHREF
 
 Searches the database for all records matching (at least) the key/value pairs
-in HASHREF.  Returns all the records found as FS::Record objects.
+in HASHREF.  Returns all the records found as `FS::TABLE' objects if that
+module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
+objects.
 
 =cut
 
@@ -166,26 +169,34 @@ sub qsearch {
   my($sth);
   my($statement) = "SELECT * FROM $table". ( @fields
     ? " WHERE ". join(' AND ',
-        map("$_ = ". _quote($record->{$_},$table,$_), @fields)
-      )
-    : ''
+      map {
+        $record->{$_} eq ''
+          ? "$_ IS NULL"
+          : "$_ = ". _quote($record->{$_},$table,$_)
+      } @fields
+    ) : ''
   );
   $sth=$dbh->prepare($statement)
     or croak $dbh->errstr; #is that a little too harsh?  hmm.
 
-  map {
-    new FS::Record ($table,$sth->fetchrow_hashref);
-  } ( 1 .. $sth->execute );
+  if ( eval ' scalar(@FS::'. $table. '::ISA);' ) {
+    map {
+      eval 'create FS::'. $table. ' ( $sth->fetchrow_hashref );';
+    } ( 1 .. $sth->execute );
+  } else {
+    cluck "qsearch: warning: FS::$table not loaded; returning generic FS::Record objects";
+    map {
+      new FS::Record ($table,$sth->fetchrow_hashref);
+    } ( 1 .. $sth->execute );
+  }
 
 }
 
 =item qsearchs TABLE, HASHREF
 
-Searches the database for a record matching (at least) the key/value pairs
-in HASHREF, and returns the record found as an FS::Record object.  If more than
-one record matches, it B<carp>s but returns the first.  If this happens, you
-either made a logic error in asking for a single item, or your data is
-corrupted.
+Same as qsearch, except that if more than one record matches, it B<carp>s but
+returns the first.  If this happens, you either made a logic error in asking
+for a single item, or your data is corrupted.
 
 =cut
 
@@ -398,7 +409,7 @@ sub rep {
   my(@diff)=grep $new->getfield($_) ne $old->getfield($_), @fields;
 
   if ( scalar(@diff) == 0 ) {
-    carp "Records identical";
+    carp "warning: records identical";
     return '';
   }
 
@@ -746,6 +757,10 @@ sub fields {
 
 =back
 
+=head1 VERSION
+
+$Id: Record.pm,v 1.9 1998-11-21 07:26:45 ivan Exp $
+
 =head1 BUGS
 
 This module should probably be renamed, since much of the functionality is
@@ -768,7 +783,7 @@ The ut_ methods should ask the dbdef for a default length.
 
 ut_sqltype (like ut_varchar) should all be defined
 
-A fallback check method should be provided with uses the dbdef.
+A fallback check method should be provided whith uses the dbdef.
 
 The ut_money method assumes money has two decimal digits.
 
@@ -862,6 +877,31 @@ added pod documentation ivan@sisd.com 98-sep-6
 
 ut_phonen got ''; at the end ivan@sisd.com 98-sep-27
 
+$Log: Record.pm,v $
+Revision 1.9  1998-11-21 07:26:45  ivan
+"Records identical" carp tells us it is just a warning.
+
+Revision 1.8  1998/11/15 11:02:04  ivan
+bugsquash
+
+Revision 1.7  1998/11/15 10:56:31  ivan
+qsearch gets sames "IS NULL" semantics as other WHERE clauses
+
+Revision 1.6  1998/11/15 05:31:03  ivan
+bugfix for new config layout
+
+Revision 1.5  1998/11/13 09:56:51  ivan
+change configuration file layout to support multiple distinct databases (with
+own set of config files, export, etc.)
+
+Revision 1.4  1998/11/10 07:45:25  ivan
+doc clarification
+
+Revision 1.2  1998/11/07 05:17:18  ivan
+In sub new, Pg wrapper for money fields from dbdef (FS::Record::fields $table),
+not keys of supplied hashref.
+
+
 =cut
 
 1;