Wednesday, June 17, 2009

QueryLog support for Catalyst::Model::DBIC::Schema

well, I wrote Catalyst::Model::DBIC::Schema::QueryLog before. it's fine. it works.

mst released the new Catalyst::Model::DBIC::Schema today. built on top of Moose, with two traits: Caching and Replicated.
It reminds me to write Catalyst::TraitFor::Model::DBIC::Schema::QueryLog
trait is great. easy to read, configurable and sane.

Enjoy. Thanks

Labels: , ,

Tuesday, May 12, 2009

DBIx::Class::ResultSet::Void and TheSchwartz::Moosified

Big thanks to ribasushi, the new DBIx::Class::ResultSet::Void 0.04 is using
# SELECT 1 FROM item me WHERE ( me.id = ? ) LIMIT 1: '1'
# INSERT INTO item ( id, name) VALUES ( ?, ? ): '1', 'A'
instead of COUNT(*). it's much faster now.

Another big thanks to stash from socialtext, TheSchwartz::Moosified 0.04 with PostgreSQL support and bug fixes.

Enjoy!

Labels: , ,

Sunday, May 10, 2009

DBIx::Class::ResultSet::Void

some time I'm really feeling sick when I use update_or_create or find_or_create, the default DBIx::Class::ResultSet return DBIx::Class::Row object by using find.
it's useful when I need the $row object, but it's dummy if I don't care.

I wrote DBIx::Class::ResultSet::Void today. It would use count instead of find if not defined wantarray. it means when I call
$rs->find_or_create( { id => 1, name => 'A' } );
instead of
my $row = $rs->find_or_create( { id => 1, name => 'A' } );
it would generate SQLs like:
# SELECT COUNT( * ) FROM item me WHERE ( me.id = ? ): '1'
# INSERT INTO item ( id, name) VALUES ( ?, ? ): '1', 'A'
it's a little better I think.

read more on the POD and Enjoy!

Labels: ,

Sunday, June 17, 2007

difference between $c->user and $c->user->obj

when we use Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user and $c->user->obj. but most of people are confusing with them I guess.

differences:
* $c->user is ISA Catalyst::Plugin::Authentication::Store::DBIC::User, while $c->user->obj is just something like sub obj in Catalyst::Plugin::Authentication::Store::DBIC::User. see:
__PACKAGE__->mk_accessors(qw/id config obj store/);
# .skip
bless {
id => $id,
config => $config,
obj => $user_obj,
}, $class;

* why $c->user->username is the same as $c->user->obj->username is because of
sub AUTOLOAD {
my $self = shift;
(my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
return if $method eq "DESTROY";

$self->obj->$method(@_);
}
. when we call ->username of $c->user. it will call sub username in Catalyst::Plugin::Authentication::Store::DBIC::User. then sub AUTOLOAD works here, it call $self->obj->$method(@_) indeed. ($self is $c->user).

so there is something interesting..
* because we have sub for_session in Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user->for_session ( but not $c->user->obj->for_session if user table don't have "for_session" column ).
* when I see something "*user = \&obj;" in the code. I know I can use $c->user->user->username. it's the same as $c->user->obj->username. (JOKE!)

for most situation. I suggest use $c->user->obj->column_name because that's more direct (no AUTOLOAD involved).

Enjoy!.

Labels: ,

Catalyst and DBIx-Class tip

* Catalyst Tip: use set_authenticated instead of login when password is SHA1 hashed.

well, Foorum use SHA1 to encrypt the password. so no one knows the original password including me (db do not store original password).
so if u are trying $c->login($username, $password_sha1_hashed);, it will fail.
if u are checking the module of Catalyst::Plugin::Authentication::Credential::Password which sub login is there, we would know the solution:
$c->set_authenticated($user);

that does the same as login but no password check.

* DBIx::Class Tip: attribute offset.

sometimes when we run a SQL, that's not something like LIMIT 0,6 LIMIT 6,6
it might be something like LIMIT 5,6
well, the attributes of "rows" and "page" does something like LIMIT 6,6. but no luck with 5,6.
so if u want run a SQL with LIMIT 5,6, u might can try
search( {}, {
rows => 6,
offset => 5
} );
that would be what u want.

be careful, offset do no create a Data::Page object for you. I mean ->pager. u need create that yourself.

Enjoy!

Labels: ,

Wednesday, June 06, 2007

be careful

my @a = ( 'a', $c->req->param('a'), 'b', $c->req->param('b'), 'c', undef , 'd',, 'f' );$c->res->body(Dumper(\@a));
when we try "?b=1", output:
$VAR1 = [
'a',
'b',
'1',
'c',
undef,
'd',
'f'
];

see? $c->req->param('a') is NOT undef.
so when u try something like
$rs->update( {
column1 => $c->req->param('column1'),
column2 => $c->req->param('column2')
} );
then if column1 param is not specified, this would raise error because $rs try to update column1, column2, $c->req->param('column2'). we want 4 but u give me 3.

so man, be careful of that. :)

Labels: ,

Friday, August 25, 2006

search->first, find, and single

DBIx::Class 简单地读取一条数据的时候有三种方法。

一是用 ->search( { author_id => 1 } )->first;
二是用 ->find( { author_id => 1 } );
三是用 ->single( { author_id => 1 } );

我用 Data::Compare 比较过。三者的数据是一样的。
但是原理上来说,最下面的是最快的。因为没有创建一个 Cursor.

http://search.cpan.org/~jrobinson/DBIx-Class-0.07001/lib/DBIx/Class/ResultSet.pm#single

Labels: