Skip to content

Loading…

Use SQL::Maker::SQLType to pass around the typed parameters #133

Merged
merged 2 commits into from

3 participants

@kazuho

As discussed in #132, strict mode of SQL::Maker disallows the use of arrayrefs as bind parameters, and thus Teng needs to use SQL::Maker::SQLType for passing the typed parameters around.

Note: Programming interface provided by SQL::Maker::SQLType is awkward (in the sense that a) designates scalarrefs to be used for passing in the value, b) does not provide a way to fetch the value-type pair in a style that is easy to be passed to $sth->bind_param). However after discussing with @tokuhirom, the conclusion is to not change the current API.

@cho45 cho45 merged commit eaa7580 into nekokak:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 11, 2014
  1. @nobuoka @kazuho
  2. @kazuho

    use SQL::Maker::SQLType to pass around the typed parameter (fixes #132,…

    kazuho committed
    … but why should we pass-in an value_ref to SQL::Maker::SQLType?)
Showing with 18 additions and 2 deletions.
  1. +8 −2 lib/Teng.pm
  2. +10 −0 t/002_common/001_insert.t
View
10 lib/Teng.pm
@@ -4,6 +4,8 @@ use warnings;
use Carp ();
use Class::Load ();
use DBI 1.33;
+use Scalar::Util;
+use SQL::Maker::SQLType qw(sql_type);
use Teng::Row;
use Teng::Iterator;
use Teng::Schema;
@@ -271,7 +273,11 @@ sub execute {
$sth = $self->dbh->prepare($sql);
my $i = 1;
for my $v ( @{ $binds || [] } ) {
- $sth->bind_param( $i++, ref($v) eq 'ARRAY' ? @$v : $v );
+ if (Scalar::Util::blessed($v) && ref($v) eq 'SQL::Maker::SQLType') {
+ $sth->bind_param($i++, ${$v->value_ref}, $v->type);
+ } else {
+ $sth->bind_param( $i++, $v);
+ }
}
$sth->execute();
};
@@ -307,7 +313,7 @@ sub _bind_sql_type_to_args {
for my $col (keys %{$args}) {
# if $args->{$col} is a ref, it is scalar ref or already
# sql type bined parameter. so ignored.
- $bind_args->{$col} = ref $args->{$col} ? $args->{$col} : [ $args->{$col}, $table->get_sql_type($col) ];
+ $bind_args->{$col} = ref $args->{$col} ? $args->{$col} : sql_type(\$args->{$col}, $table->get_sql_type($col));
}
return $bind_args;
View
10 t/002_common/001_insert.t
@@ -4,6 +4,7 @@ use Test::More;
my $dbh = t::Utils->setup_dbh;
my $db = Mock::Basic->new({dbh => $dbh});
+my $db_with_strict_sql_builder = Mock::Basic->new({dbh => $dbh, sql_builder_args => { strict => 1 }});
$db->setup_test_db;
subtest 'insert mock_basic data/ insert method' => sub {
@@ -15,6 +16,15 @@ subtest 'insert mock_basic data/ insert method' => sub {
is $row->name, 'perl';
};
+subtest 'insert with strict sql builder' => sub {
+ my $row = $db_with_strict_sql_builder->insert('mock_basic',{
+ id => 5,
+ name => 'python',
+ });
+ isa_ok $row, 'Teng::Row';
+ is $row->name, 'python';
+};
+
subtest 'scalar ref' => sub {
$db->suppress_row_objects(0);
my $row = $db->insert('mock_basic',{
Something went wrong with that request. Please try again.