DbConnection with UpdateRow

2 views
Skip to first unread message

Robert

unread,
Oct 21, 2008, 3:20:30 PM10/21/08
to zoop-users
Hi,

I have a quick question about return values when updating a postgresql
database. See the following code from DbConnection of Zoop version
1.51, about lines 478 to 495.

public function updateRow($sql, $params)
{
$affected = $this->updateRows($sql, $params);
if($affected == 0 || $affected > 1)
trigger_error("attempting to update one row, $affected altered");
}


public function updateRows($sql, $params)
{
$res = $this->query($sql, $params);
return $res->affectedRows();
}

The function updateRows returns the number of rows affected. However,
the function updateRow does NOT return a value. (It does verify that
only one row was affected but it does not return a value.)

My question:
Is there some reason why it does not return a value?
Wouldn't it be better if it did return a 1 for successful update of 1
row?

It appears that SqlUpdateRow ( ) in functions.php is expecting a
return value from updateRow ( ).

Thanks,
Robert J.

Rick Gigger

unread,
Nov 18, 2008, 9:25:40 PM11/18/08
to zoop-...@googlegroups.com
Sorry for taking so long to get back to you on this. I got away from
zoop for a little while and am now trying to get back into it.

On Oct 21, 2008, at 1:20 PM, Robert wrote:

>
> Hi,
>
> I have a quick question about return values when updating a postgresql
> database. See the following code from DbConnection of Zoop version
> 1.51, about lines 478 to 495.
>
> public function updateRow($sql, $params)
> {
> $affected = $this->updateRows($sql, $params);
> if($affected == 0 || $affected > 1)
> trigger_error("attempting to update one row, $affected altered");
> }
>
>
> public function updateRows($sql, $params)
> {
> $res = $this->query($sql, $params);
> return $res->affectedRows();
> }
>
> The function updateRows returns the number of rows affected. However,
> the function updateRow does NOT return a value. (It does verify that
> only one row was affected but it does not return a value.)
>
> My question:
> Is there some reason why it does not return a value?
> Wouldn't it be better if it did return a 1 for successful update of 1
> row?

Well, it seems like if it didn't throw an error than it means it
worked and updated exactly one row. If the sql is bad the db driver
will throw an error. If it doesn't update exactly one row it will
throw and error. So the only valid things it could return is that
completed successfully or that it updated exactly one row. If neither
of those things are true then an error will be thrown and you won't
even get to returning a result. Under what conditions could it get
that far without throwing and error and yet not be "successful"?

It would be very easy to do that, I am just trying to envision the use
case.


> It appears that SqlUpdateRow ( ) in functions.php is expecting a
> return value from updateRow ( ).

Yeah, we need to either have updateRow return something or have
SqlUpdateRow not return anything.

I'd love to know your thoughts on the matter.

Thanks,

Rick

Reply all
Reply to author
Forward
0 new messages