Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Dynamic query generation

by chanakya (Friar)
on May 05, 2005 at 10:19 UTC ( [id://454294]=perlquestion: print w/replies, xml ) Need Help??

chanakya has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!
I'm trying to build a query based on a "Action". The Action value comes from a SELECT query.

I have a hash which contains the database field names and there equivalent values (These values will be populated by a SELECT query).
I want to build a query from this hash based on the "Action" value.
For example if the "Action" is "Problem Submit", I'd like a build a query like

INSERT INTO Ticket_Data(Data_id, Prov_id, Provclientid) VALUES (Ticket +_Data_seq.nextval, 'EECO::UTN:BPL', 'BPC::123');
All the other fields needs to be skipped if the Action is "Problem Submit"(other than Data_id, Prov_id, Provclientid)

I'm trying to achieve the same using the code below, but could not get the values while building the query.
The reason being, to skip some of the fields I need to traverse the hash using a foreach() loop, once done I've to
append the values outside the foreach() loop to the main query.

Could anyone throw some light about how to achieve the above said functionality.
$query = $dbh->prepare(qq{SELECT * from table_name}); ... my $data = $query->fetchrow_hashref; ... %MAPPING = ( 'Data_id' => 'Ticket_Data_seq.nextval', 'Action' => "Problem Submit", 'Cust_Ticket_Number' => "", 'Our_Ticket_Number' => "$data->{'TicketNumber'}", 'msg_id' => "", 'time_stamp' => "", 'Description' => "", 'EL_Edesc' => "", 'prov_id' => "EECO::UTN::BPL", 'provclientid' => "BPC::123", 'prov_client_org' => "", 'prov_client_buz' => "", 'prov_schema_type' => "" ); if ($action eq 'Problem Submit') my $query = "INSERT INTO Ticket_Data ("; for $key ( keys %ORA_MAPPING ) { $value = $ORA_MAPPING{$key}; next if $key eq "Data_id"; $q.=" $key, "; } $query .= " ) VALUES ($ORA_MAPPING{$_} )"; } elsif ($action eq 'Update') { ... }
!!@!!
May the force be with you!!

Replies are listed 'Best First'.
Re: Dynamic query generation
by Joost (Canon) on May 05, 2005 at 10:32 UTC
    You should be using strict. I think $q.="$key, "; should be $query.="$key, ";

    update: and you're creating a new lexical variable $query within the if ($action eq ..., which goes out of scope.

    my $query; if ($action eq 'Problem Submit') $query = "INSERT INTO Ticket_Data ("; for $key ( keys %ORA_MAPPING ) { my $value = $ORA_MAPPING{$key}; next if $key eq "Data_id"; $query.=" $value, "; # or did you really mean $key here? }
    Also note that you should be careful if you build queries from external/dynamic input. Use placeholders if you can, and DBI->quote otherwise.

      Joost
      Thanks for pointing out the error,
      Here's the updated code
      %ORA_MAPPING = ( 'Data_id' => 'Ticket_Data_seq.nextval', 'Action' => "Problem Submit", 'Cust_Ticket_Number' => "", 'Our_Ticket_Number' => "$data->{'TicketNumber'}", 'msg_id' => "", 'time_stamp' => "", 'Description' => "", 'EL_Edesc' => "", 'prov_id' => "EECO::UTN::BPL", 'provclientid' => "BPC::123", 'prov_client_org' => "", 'prov_client_buz' => "", 'prov_schema_type' => "" ); my $query = "INSERT INTO Ticket_Data ("; if ($action eq 'Problem Submit') for $key ( keys %ORA_MAPPING ) { $value = $ORA_MAPPING{$key}; next if $key eq "Data_id"; $query .=" $key, "; } $query .= " ) VALUES ($ORA_MAPPING{$_} )"; } elsif ($action eq 'Update') { ... }
      Is it possible to build the query using the above code or is there any other better approach.
      I'd like to know that approach as well.
      Many thanks for your time
Re: Dynamic query generation
by polettix (Vicar) on May 05, 2005 at 10:52 UTC
    In addition to Joost's correct comments, also be careful to use the same identifier $query to represent semantically different concepts: at the very beginning it's a statement handle object, while it becomes a string containing the query to be executed later.

    I also don't entirely get the meaning of MAPPING, which automagically becomes ORA_MAPPING inside the cycle - this definitevely poses a requirement to you: use strict :)

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: Dynamic query generation
by ghenry (Vicar) on May 06, 2005 at 09:58 UTC

    Have you thought about TRIGGERS?

    I may be missing the point slightly though.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://454294]
Approved by Mutant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found