[ic] Getting database handle in UserTag

Kevin Walsh kevin at cursor.biz
Fri Jul 21 16:05:37 EDT 2006


Carl Bailey <carl at triangleresearch.com> wrote:
> Quoting Mike Heins:
> > In a global UserTag, it is:
> >
> > 	my $db = dbref('products')
> > 		or die "no products table";
> > 	my $dbh = $db->dbh()
> > 		or die "not a DBI table";
> >
> > In a catalog UserTag, there is no reliable way to do it
> > because of Safe restrictions.
> >
> We have successfully used the following in catalog UserTags:
> 
>      my $db  = Vend::Data::database_exists_ref('products')
>         or return sprintf("Table %s is not available", 'products');
>      my $dbh = $db->dbh();
> 
> It may not be "proper" but it has worked successfully for years, which 
> has been reliable enough for our purposes.
> 
That works, as does the shorthand (without Vend::Data), as follows:

    my $db = database_exists_ref('sometable') or die 'Aaaargh!';
    $db->query(...);

Also, "dbref('sometable')" does work in both local and global UserTags.

One poster said to do this, or asked whether it could be done:

    $Db{sometable}->query(...);

That should never be used because if the 'sometable' table is not
"open" then an attempt will be made to call the query() method on
an undefined object.

A more reliable way to use %Db, in a UserTag, is as follows:

    my $db;
    $db = $Db{sometable} or do {
        $Tag->perl({ table => 'sometable' });
        $db = $Db{sometable} or die 'Aaaargh!';
    };
    $db->query(...);

The $Tag->perl() call will "open" the table (if it has been defined) and
will make it available from %Db.

I tend to just use dbref() in local UserTags.  That will not work in
ActionMaps, so use the $Tag->perl() method instead, in that case.

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin at cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/


More information about the interchange-users mailing list