http://search.cpan.org/~bricas/DBIx-Class-0.07003/lib/DBIx/Class/Manual/Intro.podの抜き出しメモ(間違っても翻訳ではない).で,Schemaへの接続から
#これでいいんじゃないかな? my $another_schema = My::Schema->connect( $dsn, $user, $password, $attrs, { on_connect_do => \@on_connect_sql_statments } );
詳しくはこちら→http://search.cpan.org/~bricas/DBIx-Class-0.07003/lib/DBIx/Class/Storage/DBI.pm
基本的な使い方
## R #いちばん簡単プライマリキィで呼び出し my $album = $schema->resultset('Album')->find(14); ## U #取得したデータに値を設定してupdateできるよ $album->title('Physical Graffiti'); my $title = $album->title; # $title holds 'Physical Graffiti' #自動設定されてるアクセサを使ってもできるね $album->set_column('title', 'Presence'); $title = $album->get_column('title'); #んで,コミット $album->update; #ロールバックかな #is_changedで変更されているかわかるよ $album->discard_changes if $album->is_changed; ## C ##追加する my $new_album = $schema->resultset('Album')->create({ title => 'Wish You Were Here', artist => 'Pink Floyd' }); #すぐにアップデートできるよ $new_album->label('Capitol'); $new_album->year('1975'); $new_album->update; ## D #もちろん削除も $new_album->delete; #直接ResultSetから削除も # Delete all of Falco's albums $schema->resultset('Album')->search({ artist => 'Falco' })->delete;
データの探し方(戦い方ではない)
いや,ある意味戦いか…
#artistで検索 # Find all of Santana's albums my $rs = $schema->resultset('Album')->search({ artist => 'Santana' }); #最初のください my $album = $rs->first; print $album->title; #ループでアップデート while (my $album = $rs->next) { print $album->artist . ' - ' . $album->title; $album->year(2001); $album->update; } #まとめてアップデートもできるよ $rs->update({ year => 2001 }); #リストコンテキストだと該当行を全部返します # Fetch immediately all of Carlos Santana's albums my @albums = $schema->resultset('Album')->search( { artist => 'Carlos Santana' } ); foreach my $album (@albums) { print $album->artist . ' - ' . $album->title; } # LIKEで探す # Find albums whose artist starts with 'Jimi' my $rs = $schema->resultset('Album')->search_like({ artist => 'Jimi%' }); # WHERE節を自分で作ったり # Find Peter Frampton albums from the year 1986 my $where = 'artist = ? AND year = ?'; my @bind = ( 'Peter Frampton', 1986 ); my $rs = $schema->resultset('Album')->search_literal( $where, @bind ); #もっと複雑なのがいいならSQL::Abstractを.なんでもできる(はず) my $rs = $schema->resultset('Album')->search({ artist => { '!=', 'Janis Joplin' }, year => { '<' => 1980 }, albumid => [ 1, 14, 15, 65, 43 ] }); #上のはこういうSQL書くのと同じこと #WHERE artist != 'Janis Joplin' # AND year < 1980 # AND albumid IN (1, 14, 15, 65, 43) #ハッシュでattributesを指定して検索の条件を指定したり my @albums = My::Schema->resultset('Album')->search( { artist => 'Bob Marley' }, { rows => 2, order_by => 'year DESC' } );
もっとサンプル→
http://search.cpan.org/~bricas/DBIx-Class-0.07003/lib/DBIx/Class/Manual/Cookbook.pod
これを見よう→
http://search.cpan.org/~bricas/DBIx-Class-0.07003/lib/DBIx/Class/ResultSet.pm
http://search.cpan.org/~nwiger/SQL-Abstract-1.22/lib/SQL/Abstract.pm