Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: SQL::Abstract 'between' clause (about done)

by TJPride (Pilgrim)
on Nov 25, 2011 at 11:40 UTC ( [id://940032]=note: print w/replies, xml ) Need Help??


in reply to SQL::Abstract 'between' clause (about done)

Sorry for asking possibly stupid questions, but why use a complicated library for something like this when you can just write the query directly and insert safed values? Something like this:

use strict; use warnings; ### Matches negatives, decimals sub isNum { return $_[0] =~ m/^-?(?:\d*\.)?\d+$/; } ### Converts non-safe characters in string sub safeString { my $val = $_[0]; $val =~ s/(['"\\\x00\x1a])/\\$1/g; return $val; } ### Determines type of value, converts string sub safeValue { my $val = $_[0]; return "'" . safeString($val) . "'" if length($val) == 0 || !isNum($val); return $val; } ### Safes all inserted values ### Inserts strings with quotes if {key} is used ### Inserts without quotes if [key] is used sub queryMysql { my ($query, $args) = @_; $query =~ s/\[(\w+)\]/safeString($args->{$1})/eg; $query =~ s/{(\w+)}/safeValue($args->{$1})/eg; return $query; } my %values = ( 'table' => 'tablename', 'x' => '123', 'y' => 'alpha', 'z' => -45.67, 'colname' => 'COLB', 'from' => '20110101', 'to' => '20110131' ); print queryMysql(' UPDATE [table] SET x = {x}, y = {y}, z = {z} WHERE [colname] BETWEEN {from} AND {to}', \%values); ### UPDATE tablename ### SET x = 123, y = 'alpha', z = -45.67 ### WHERE COLB BETWEEN 20110101 AND 20110131
I hacked this together years ago for situations where I don't want to bother messing with the ? ? and separate array format of DBI. Could probably take a few minutes and write a function for generating inserts and updates as well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found