[ic] SQL Query in UserTag

Ben Mirecki interchange-users@lists.akopia.com
Mon May 14 15:01:00 2001


Ok, now here's what my code looks like:

UserTag calc_price Order sku framesize frametype
UserTag calc_price Interpolate
UserTag calc_price HasEndTag
UserTag calc_price Routine <<EOR
sub {

  my $get_dbh = sub {
    my ($table_name) = @_;
    my $db = Vend::Data::database_exists_ref($table_name);
    return $db->[$Vend::Table::DBI::DBI];
  };

  my $dbh = $get_dbh->('framestyles');
  my $sql_query = "SELECT price FROM framestyles WHERE sku = '9848'";
  my $sth = $dbh->prepare($sql_query) or return error_message("Can't open
framestyles database");

  return $dbh;

}
EOR

It is in fact creating the handle and returning it, but I'm getting an error
saying, "Runtime error: Can't call method "prepare" on unblessed reference
at (eval 233) line 11." That disappears when I comment out the my $sth...
line.


On Mon, May 14, 2001 at 10:21:40AM -0400, Ben Mirecki wrote:
> Hi!
>=20
> I'm trying to create a UserTag that does a field value lookup from an
> arbitrary table. The UserTag will be called from CommonAdjust as part of =
the
> pricing method.

I just did this, so perhaps I can help :)

>=20
> Here's the code I have.
>=20
> UserTag calc_price Order
> UserTag calc_price Routine <<EOR
> sub {
>   my $dbh =3D $Sql{framestyles};
>   my $sql_query =3D "SELECT price FROM framestyles WHERE sku =3D 9848";
>   my $sth =3D $dbh->prepare($sql_query) or return error_message("Error...=
");
>   my $rc =3D $sth->execute() or return HTML("Error...");
>=20
>   return 0;
> }
> EOR
>=20

You can't access %Sql like that from a usertag. I wrote a small sub-routine
to get me a database handle.

my $get_dbh =3D sub {
      my ($table_name) =3D @_;
      my $db =3D Vend::Data::database_exists_ref($table_name);
      return $db->[$Vend::Table::DBI::DBI];
   };

I define this at the top of my user tag, and then I use it like this:

my $dbh =3D $get_dbh->('database_name');


You should now be able to replace your line

my $dbh =3D $Sql{framestyles};

with my line

my $dbh =3D $get_dbh->('framestyles');

and it should work the way you think it will.

--=20
Start random buzzword text:
Thus, the product configuration baseline adds overriding performance constr=
aints to gain the full benefts of the client-server archetecture.=20