Last active
August 29, 2015 14:03
-
-
Save ytnobody/5ef63c4c2156a55d6b37 to your computer and use it in GitHub Desktop.
an example for using SQL::Maker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use SQL::Maker; | |
my $maker = SQL::Maker->new(driver => 'mysql'); | |
my ($sql, @binds) = $maker->select( | |
'book', | |
['*'], | |
{price => {'<=' => 1000}}, | |
{order => 'price DESC'} | |
); | |
### クエリとバインド値の出力 | |
printf "*** QUERY ***\n%s\n\n\b*** BINDS ***\n%s\n\n", $sql, join(', ', @binds); | |
### 無理やりクエリにバインド値を当て込む場合。(perl 5.14.0以降のみ有効) | |
my $query_format = $sql =~ s/\?/\'\%s\'/gr; | |
my $raw_sql = sprintf($query_format, @binds); | |
printf "*** RAW_SQL ***\n%s\n\n", $raw_sql; | |
__DATA__ | |
実行結果。 | |
$ perl sqlmaker.pl | |
*** QUERY *** | |
SELECT * | |
FROM `book` | |
WHERE (`price` <= ?) | |
*** BINDS *** | |
1000 | |
*** RAW_SQL *** | |
SELECT * | |
FROM `book` | |
WHERE (`price` <= '1000') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment