[freeside-devel] FS::part_export::sqlradius::_export_replace

ivan ivan at 420.am
Tue May 14 00:37:24 PDT 2002


On Mon, May 13, 2002 at 08:42:35PM -0700, Kristian Hoffmann wrote:
> On Mon, 13 May 2002, ivan wrote:
> 
> > It shouldn't do that.  Although, admittedly, the naming of the `insert'
> > subroutine is confusing, take an actual look at the code - it's supposed
> > to attempt to update an existing value before inserting a new one:
>
> When it does and UPDATE on any of the items that haven't changed, the
> return value is 0 because nothing was changed.  It then falls through to
> the INSERT and adds a duplicate row.  If you specify
> "mysql_client_found_rows=1" in the datasrc, DBD::mysql::execute returns
> the number of rows that were matched, not updated, when doing an UPDATE.
> 
> Soooo, update TFM or TFS?

Hmm, I think it was a bad idea to use the return value of the update
command in the first place.  Maybe try this (or grab sqlradius.pm from
CVS) and let me know if it fixes the problem:

Index: sqlradius.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_export/sqlradius.pm,v
retrieving revision 1.2
diff -u -r1.2 sqlradius.pm
--- sqlradius.pm        2002/04/17 20:43:45     1.2
+++ sqlradius.pm        2002/05/14 07:30:24
@@ -108,18 +108,34 @@
 
 sub sqlradius_insert { #subroutine, not method
   my $dbh = sqlradius_connect(shift, shift, shift);
-  my( $replycheck, $username, %attributes ) = @_;
+  my( $table, $username, %attributes ) = @_;
 
   foreach my $attribute ( keys %attributes ) {
-    my $u_sth = $dbh->prepare(
-      "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?"    ) or die $dbh->errstr;
-    my $i_sth = $dbh->prepare(
-      "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
-        "VALUES ( ?, ?, ?, ? )"
+  
+    my $s_sth = $dbh->prepare(
+      "SELECT COUNT(*) FROM rad$table WHERE UserName = ? AND Attribute = ?"
     ) or die $dbh->errstr;
-    $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
-      or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
-        or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
+    $s_sth->execute( $username, $attribute ) or die $s_sth->errstr;
+
+    if ( $s_sth->fetchrow_arrayref->[0] ) {
+
+      my $u_sth = $dbh->prepare(
+        "UPDATE rad$table SET Value = ? WHERE UserName = ? AND Attribute = ?"
+      ) or die $dbh->errstr;
+      $u_sth->execute($attributes{$attribute}, $username, $attribute)
+        or die $u_sth->errstr;
+
+    } else {
+
+      my $i_sth = $dbh->prepare(
+        "INSERT INTO rad$table ( id, UserName, Attribute, Value ) ".
+          "VALUES ( ?, ?, ?, ? )"
+      ) or die $dbh->errstr;
+      $i_sth->execute( '', $username, $attribute, $attributes{$attribute}
)
+        or die $i_sth->errstr;
+
+    }
+
   }
   $dbh->disconnect;
 }

-- 
_ivan



More information about the freeside-devel mailing list