Re: [PHP-PEAR] PHPLib and Pear merge

[email protected] ((Kristian Koehntopp)) 10 Mar 2001 11:57:35 -0000
Newsgroups netuse.lists.php-pear
Message-ID <[email protected]>
In netuse.lists.php-pear you write:
>The PHPLIB way:

>  function deleteCat($cat_id)
>  {
>    global $db;
>    $sql = "DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'";
>   if ($db->query($sql)) {
>     return true;
>   } else {
>      return false;
>   }
>  }

PHPLIB way would be

  function deleteCat($cat_id) {
    $db = new DB_mydatabase("DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'");
    return $db->affected_rows();
  }

PHPLIB DB_Sql objects are cheap, PHP connection reuse takes care
of the rest. The PHPLIB API is optimized for terse code.

Actually, it is still to verbose for me, I'd like to have
Objective-C and Nextstep syntax, as in

  function deleteCat($cat_id) {
    return [[ DB_mydatabase fromQuery: "DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'" ] affectedRows ];
  }

That is, the DB_mydatabase constructor fromQuery: would return a
database object instance, with would then be sent an
affectedRows message. The result of that message can then be
returned. In PHP syntax, that would become

  return $DB_mydatabase::from_query("DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'")->affected_rows();

only that this is not PHP syntax. :-)

>The PEAR: DB way:

>  function deleteCat($cat_id)
>  {
>    global $db;
>    $sql = "DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'";
>    $sth = $db->query($sql);
>    if (DB::isError($sth)) {
>      // print $sth->getMessage();
>      return false;
>    } else {
>     return true;
>    }
>  }

>** Second example

>PHPLIB way of dumping records

>     $db->query($sql);
>     while($db->next_record()) {
>       $cat_id = $db->f('cat_id');
>       $cat_pos = $db->f('cat_pos');
>       $cat_name = $db->f('cat_name');

Can even be written as

  $db->query($sql);
  while ($db->next_record()) {
    $cat_id   = $db->Record['cat_id'];
    $cat_pos  = $db->Record['cat_pos'];
    $cat_name = $db->Record['cat_name'];
  }

to make it more similar to PEAR, although that is not
recommended (there is no guarantee that all DB_Sql
implementations actually support a Record[] instance variable.

In retrospect, I would write it a bit differently, though, so
that you _optionally_ can get your database results indexed
numerically and only numerically (as in mysql_fetch_array( ...,
MYSQL_NUM). Then you could to a

  $db->query($sql);
  while(list($cat_id, $cat_pos, $cat_name) = $db->next_record()) {
    ...
  }

which would be handy in many cases. Also, some of the later
DB_Sql database interfaces implement a $db->value() method, so
that SQL statements returning only a 1x1 table can be run
directly:

  $num = $db->value("select count(*) as num from table");

which is often very handy.

As I said, DB_Sql did not start out as a database abstraction,
but as a database wrapper, making my code terser and better
error checked. There is no such thing as "abstraction" as long
as you deal with SQL.

Kristian