[ic] Tag->query in a usertag

Ron Phipps interchange-users@interchange.redhat.com
Fri Apr 19 02:56:01 2002


> From: interchange-users-admin@interchange.redhat.com
[mailto:interchange-
> users-admin@interchange.redhat.com] On Behalf Of Kevin Walsh
> 
> > UserTag get-valid-qty-break-fields Routine <<EOR
> > sub {
> > 	my $code = 'test-001';
> > 	my $available_breaks =
> > "q2,q3,q4,q5,q6,q7,q8,q9,q10,q12,q15,q25,q50,q100";
> >
> > 	my $break;
> > 	my $sql = "SELECT " . $available_breaks . " FROM pricing WHERE
> > sku ='" . $code . "';";
> >
> > 	my $sql_results = $Tag->query( {sql => $sql, hashref =>
> > 'price_results' } );
> >
> > 	my $out = ref($sql_results);
> >
> > 	return $out;
> > }
> > EOR
> >
> > The inline perl returns 'ARRAY' as it should and the usertag returns
''.
> > There are no errors in the error log, however it appears that the
call
> > to $Tag->query is not working in the usertag.
> >
> > I'm not too sure where to go from here, all of my tests show the
code
> > working inline, but fail as a usertag without violating safe
> > constraints.  Any ideas?
> >
> Remove this line:
> 
>     my $sql_results = $Tag->query( {sql => $sql, hashref =>
> 'price_results' } );
> 
> and replace with the following:
> 
>     my $db = ::database_exists_ref('pricing') or die 'Bad pricing
table';
>     my $sql_results = $db->query({ sql => $sql, hashref =>
'price_results'
> });

Thanks Kevin.  So the reason why $Tag->query worked in the inline perl
was because of the tables=pricing in the perl call?  And in a usertag
since you do not have that association already you must create a db
object which points to the table?  Makes a lot of sense now!

> 
> By the way, there's no need for the ';' at the end of the SELECT
> statement.

Ahh, only needed for multiple statements, I guess it's a habit ;)

> 
> Also, if you are going to go this way, you may as well create your own
> price break table.  A table with three columns should do the trick:
> 
>     sku             qty     price
>     --------------- ------- -------
>     test-001        2       100.00
>     test-001        3       95.00
>     test-001        10      50.00
>     test-001        50      20.00
>     test-999        5       200.00
>     test-999        10      150.00
> 
> You'll find that a table like that will give you a lot more
flexibility.
> You won't have to use the same price breaks for every product and you
> won't have to edit the UserTag every time you add a new price break.
> Just something for you to think about.

I agree and I've always wanted to do it this way, the piece of code I'm
building is for a client that already has their data setup in the
existing structure so I'll probably continue down that road.  However I
think I will implement this for a new project I'm working on.  Maybe
even make it cool by putting a flag in the products db that controls
whether the break is only given on the requested quantity or if the
break is for all quantities equal to and greater then the break point
(the whole reason for this deal in the first place).

Thanks again for your insight,
-Ron