[ic] SQL Query in UserTag

David Totten interchange-users@lists.akopia.com
Tue May 15 11:40:01 2001


On Tue, May 15, 2001 at 10:20:22AM -0400, Ben Mirecki wrote:
> David,
> 
> Please forgive my ignorance, but I still can't seem to get this working. At
> this point, I have no desire to understand how this works :) All I need is a
> [calc_price] tag that I can pass a sku, framesize, and frametype to that
> will do a simple lookup and return a price value. That's it.

hehe.. I know how frustrating this can be at times.

> 
> With the code below, the tag doesn't even return 5 (the hard coded value)
> which suggests to me that something is majorly wrong. Instead, I see the
> actual tag code (ie. "[calc_price 9848 8x10]") in the html output.
> 

yeah, there are problems with the code.. I posted a "working on my system"
usage of $Tag->query()

> If we ever get this working, I'll have to buy you a coffee for sure! I'm
> from the Toronto area, but I travel often so maybe we could hook up
> sometime.

hehe.. thanks for the offer, but not neccessary

> UserTag calc_price Order sku framesize frametype
> UserTag calc_price Interpolate
> UserTag calc_price HasEndTag
> UserTag calc_price Routine <<EOR
> sub {
> 
>   my $sql = "SELECT price FROM framestyles WHERE sku = 9848";
>   my $result_array = $Tag->query( { sql => $sql, }, $body );
>   my ($same_results, $col_name_hash, $col_name_array) = $Tag->query( { sql
> => $sql, }, $body);
>   my $result_hasharray = $Tag->query( { sql => $sql, hashref =>
> 'my_results', }, $body );
> 
>   return 5.00;  # test value
> }
> EOR

Since I didn't have a copy of your database, I just did a simple query of
select price from products.

UserTag calc_price Order framesize frametype
UserTag calc_price Interpolate
UserTag calc_price HasEndTag
UserTag calc_price Routine <<EOR
sub {
   my $return = "";
   my $sql = "select price from products";

   #here you must use the wantarray => 1, cause the documentation says so :)
   #The documentation also says that using that will return an arrayref of your
   #results, a hashref of the column names, and an arrayref of the column names.   my ($results,$colname_hashref,$colname_arrayref) = $Tag->query({ wantarray => 1,sql => $sql, } );

   #take the arrayref, and convert to an array @results, which is an array of
   #arrayrefs.
   my @results = @{$results};
   foreach my $ref (@results) {
      my @tmp = @{$ref}; #dereference each arrayref
      foreach my $test (@tmp) { #@temp now holds actual (dereferenced) results
         $return .= "$test\n";
      }
   }

   return $return;

}
EOR


Dave Totten

-- 
Start random buzzword text:
Interestingly enough, an enterprise java beans based solution maximizes the probability of project success, yet minimizes cost and time required for the corporate intranet.