dangling cust_credit_refund not allowed
[freeside.git] / FS / FS / Record.pm
index 08811a2..94cc356 100644 (file)
@@ -563,6 +563,17 @@ sub dbdef_table {
   dbdef->table($table);
 }
 
+=item primary_key
+
+Returns the primary key for the table.
+
+=cut
+
+sub primary_key {
+  my $self = shift;
+  my $pkey = $self->dbdef_table->primary_key;
+}
+
 =item get, getfield COLUMN
 
 Returns the value of the column/field/key COLUMN.
@@ -677,6 +688,24 @@ sub modified {
   $self->{'modified'};
 }
 
+=item select_for_update
+
+Selects this record with the SQL "FOR UPDATE" command.  This can be useful as
+a mutex.
+
+=cut
+
+sub select_for_update {
+  my $self = shift;
+  my $primary_key = $self->primary_key;
+  qsearchs( {
+    'select'    => '*',
+    'table'     => $self->table,
+    'hashref'   => { $primary_key => $self->$primary_key() },
+    'extra_sql' => 'FOR UPDATE',
+  } );
+}
+
 =item insert
 
 Inserts this record to the database.  If there is an error, returns the error,
@@ -688,6 +717,8 @@ sub insert {
   my $self = shift;
   my $saved = {};
 
+  warn "$self -> insert" if $DEBUG;
+
   my $error = $self->check;
   return $error if $error;
 
@@ -784,8 +815,7 @@ sub insert {
         dbh->rollback if $FS::UID::AutoCommit;
         return dbh->errstr;
       };
-      #$i_sth->execute($oid) or do {
-      $i_sth->execute() or do {
+      $i_sth->execute() or do { #$i_sth->execute($oid)
         dbh->rollback if $FS::UID::AutoCommit;
         return $i_sth->errstr;
       };
@@ -1297,32 +1327,49 @@ null.  If there is an error, returns the error, otherwise returns false.
 
 =cut
 
+#false laziness w/ut_ipn
 sub ut_floatn {
+  my( $self, $field ) = @_;
+  if ( $self->getfield($field) =~ /^()$/ ) {
+    $self->setfield($field,'');
+    '';
+  } else {
+    $self->ut_float($field);
+  }
+}
+
+=item ut_sfloat COLUMN
+
+Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
+May not be null.  If there is an error, returns the error, otherwise returns
+false.
+
+=cut
+
+sub ut_sfloat {
   my($self,$field)=@_ ;
-  ($self->getfield($field) =~ /^(\d*)$/ ||
-   $self->getfield($field) =~ /^(-?\d+\.\d+)$/ ||
+  ($self->getfield($field) =~ /^(-?\d+\.\d+)$/ ||
    $self->getfield($field) =~ /^(-?\d+)$/ ||
-   $self->getfield($field) =~ /^(-?\d+\.\d+e\d+)$/ ||
-   $self->getfield($field) =~ /^(-?\d+e\d+)$/)
+   $self->getfield($field) =~ /^(-?\d+\.\d+[eE]-?\d+)$/ ||
+   $self->getfield($field) =~ /^(-?\d+[eE]-?\d+)$/)
     or return "Illegal or empty (float) $field: ". $self->getfield($field);
   $self->setfield($field,$1);
   '';
 }
-=item ut_floatn COLUMN
+=item ut_sfloatn COLUMN
 
-Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May be
+Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May be
 null.  If there is an error, returns the error, otherwise returns false.
 
 =cut
 
-#false laziness w/ut_ipn
-sub ut_floatn {
+sub ut_sfloatn {
   my( $self, $field ) = @_;
   if ( $self->getfield($field) =~ /^()$/ ) {
     $self->setfield($field,'');
     '';
   } else {
-    $self->ut_float($field);
+    $self->ut_sfloat($field);
   }
 }