Skip to content

Instantly share code, notes, and snippets.

@clairvy
Created September 8, 2010 12:39
Show Gist options
  • Save clairvy/570070 to your computer and use it in GitHub Desktop.
Save clairvy/570070 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib qq($FindBin::Bin/lib);
use Data::Dumper;
# preapare table
# $ sqlite3 user.db 'CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname, t_mod, user_id);'
# $ sqlite3 user.db 'INSERT INTO user (nickname, user_id) VALUES ("issm", 100);'
# preapare table
# $ mysql -e "CREATE TABLE user (nickname TEXT, t_mod TIMESTAMP, user_id INT)" test
# $ mysql -e 'INSERT user (nickname, user_id) VALUES ("issm", 100)' test
package Your::Model;
use DBIx::Skinny setup => {
# dsn => 'dbi:SQLite:dbname=user.db',
dsn => 'DBI:mysql:database=test',
username => '',
password => '',
};
1;
package Your::Model::Schema;
use DBIx::Skinny::Schema;
# use DateTime;
# use DateTime::Format::Strptime;
# use DateTime::Format::MySQL;
# use DateTime::TimeZone;
#
# my $timezone = DateTime::TimeZone->new(name => 'Asia/Tokyo');
# install_inflate_rule '^t_mod$' => callback {
# inflate {
# my $value = shift;
# my $dt = DateTime::Format::Strptime->new(
# pattern => '%Y-%m-%d %H:%M:%S',
# time_zone => $timezone,
# )->parse_datetime($value);
# return DateTime->from_object( object => $dt );
# };
# deflate {
# my $value = shift;
# return DateTime::Format::MySQL->format_datetime($value);
# };
# };
install_table user => schema {
pk 'id';
columns qw/
id
nickname
t_mod
user_id
/;
trigger pre_update => sub {
my ( $class, $args ) = @_;
# $args->{t_mod} ||= DateTime->now;
$args->{t_mod} ||= time;
};
};
1;
package main;
my $row = Your::Model->new;
# print Data::Dumper->Dump([\$row]);
$row->update(
'user',
{nickname => 'issm'},
{user_idx => 9}, # ワザとミス.ホントは user_id
);
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
my $result = `./a.pl 2>&1`;
$result =~ s/at .*lib/at lib/;
$result =~ s/[0-9]{9,}/xxTIMESTAMPxx/;
my $expect = <<'EOL';
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ DBIx::Skinny 's Exception @@@@@
Reason : DBD::mysql::st execute failed: Unknown column 'user_idx' in 'where clause' at lib/DBIx/Skinny.pm line 759.
SQL : UPDATE user SET `nickname` = ?, `t_mod` = ? WHERE (user_idx = ?)
BIND : $VAR1 = [
'issm',
xxTIMESTAMPxx,
9
];
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
at ./a.pl line 73
EOL
if (`grep VERSION lib/DBIx/Skinny.pm` =~ m/0.0719/) {
$expect =~ s/759/753/;
}
is($result, $expect, 'DBIx::Skinny value include arrayref on update ???');
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment