Skip to content

Commit

Permalink
bind valueもlogとるようにしてみた
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.coderepos.org/share/lang/perl/DBIx-Skinny/trunk@28425 d0d07461-0603-4401-acd4-de1884942a52
  • Loading branch information
nekokak committed Jan 14, 2009
1 parent 9bcbb37 commit fc9ff22
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/DBIx/Skinny.pm
Expand Up @@ -159,7 +159,7 @@ sub single {
sub search_by_sql {
my ($class, $sql, $bind, $opt_table_info) = @_;

$class->profiler->record_query($sql);
$class->profiler->record_query($sql, $bind);
my $sth = $class->_execute($sql, $bind);
return $class->_get_sth_iterator($sql, $sth, $opt_table_info);
}
Expand Down Expand Up @@ -229,7 +229,7 @@ sub insert {
$sql .= '(' . join(', ', @cols) . ')' . "\n" .
'VALUES (' . join(', ', ('?') x @cols) . ')' . "\n";

$class->profiler->record_query($sql);
$class->profiler->record_query($sql, \@bind);
my $sth = $class->_execute($sql, \@bind);

my $id = $class->attribute->{dbd}->last_insert_id($class->dbh, $sth);
Expand Down Expand Up @@ -269,7 +269,7 @@ sub update {

my $sql = "UPDATE $table SET " . join(', ', @set) . ' ' . $stmt->as_sql_where;

$class->profiler->record_query($sql);
$class->profiler->record_query($sql, \@bind);
$class->_execute($sql, \@bind);

for my $col (@{$class->schema->schema_info->{$table}->{columns}}) {
Expand Down Expand Up @@ -297,7 +297,7 @@ sub delete {
$class->_add_where($stmt, $where);

my $sql = "DELETE " . $stmt->as_sql;
$class->profiler->record_query($sql);
$class->profiler->record_query($sql, $stmt->bind);
$class->_execute($sql, $stmt->bind);

$class->call_schema_trigger('post_delete', $table);
Expand Down
2 changes: 1 addition & 1 deletion lib/DBIx/Skinny/DBD/mysql.pm
Expand Up @@ -31,7 +31,7 @@ sub bulk_insert {
my $values = '(' . join(', ', ('?') x @cols) . ')' . "\n";
$sql .= join(',', ($values) x (scalar(@bind) / scalar(@cols)));

$skinny->profiler->record_query($sql);
$skinny->profiler->record_query($sql, \@bind);
$skinny->_execute($sql, \@bind);

return 1;
Expand Down
9 changes: 6 additions & 3 deletions lib/DBIx/Skinny/Profiler.pm
Expand Up @@ -25,11 +25,14 @@ sub _normalize {
}

sub record_query {
my ($self, $sql) = @_;
my ($self, $sql, $bind) = @_;

$sql = _normalize($sql);
my $log = _normalize($sql);
if (my $bind_value = join ', ', @{$bind||[]} ) {
$log .= ' :binds ' . $bind_value;
}

push @{ $self->query_log }, $sql;
push @{ $self->query_log }, $log;
}

1;
Expand Down
18 changes: 18 additions & 0 deletions t/202_profiler.t
Expand Up @@ -38,5 +38,23 @@ describe 'profiler test' => run {
$profiler->reset;
is_deeply $profiler->query_log, [];
};

test 'recorde bind values' => run {
$profiler->record_query(q{
SELECT id FROM user WHERE id = ?
},[1]);
is_deeply $profiler->query_log, [
q{SELECT id FROM user WHERE id = ? :binds 1},
];

$profiler->record_query(q{
SELECT id FROM user WHERE (id = ? OR id = ?)
},[1, 2]);

is_deeply $profiler->query_log, [
q{SELECT id FROM user WHERE id = ? :binds 1},
q{SELECT id FROM user WHERE (id = ? OR id = ?) :binds 1, 2},
];
};
};

0 comments on commit fc9ff22

Please sign in to comment.