Skip to content

Latest commit

 

History

History
599 lines (361 loc) · 14.8 KB

File metadata and controls

599 lines (361 loc) · 14.8 KB

NAME

Class::DBI::Sweet - 膣���泣�ŝ����勉��������膣���泣�˨�����

SYNOPSIS

package MyApp::DBI;
use base 'Class::DBI::Sweet';
MyApp::DBI->connection('dbi:driver:dbname', 'username', 'password');

package MyApp::Article;
use base 'MyApp::DBI';

use DateTime;

__PACKAGE__->table('article');
__PACKAGE__->columns( Primary   => qw[ id ] );
__PACKAGE__->columns( Essential => qw[ title created_on created_by ] );

__PACKAGE__->has_a(
    created_on => 'DateTime',
    inflate    => sub { DateTime->from_epoch( epoch => shift ) },
    deflate    => sub { shift->epoch }
);


# Simple search

MyApp::Article->search( created_by => 'sri', { order_by => 'title' } );

MyApp::Article->count( created_by => 'sri' );

MyApp::Article->page( created_by => 'sri', { page => 5 } );

MyApp::Article->retrieve_all( order_by => 'created_on' );


# More powerful search with deflating

$criteria = {
    created_on => {
        -between => [
            DateTime->new( year => 2004 ),
            DateTime->new( year => 2005 ),
        ]
    },
    created_by => [ qw(chansen draven gabb jester sri) ],
    title      => {
        -like  => [ qw( perl% catalyst% ) ]
    }
};

MyApp::Article->search( $criteria, { rows => 30 } );

MyApp::Article->count($criteria);

MyApp::Article->page( $criteria, { rows => 10, page => 2 } );

MyApp::Article->retrieve_next( $criteria,
                                 { order_by => 'created_on' } );

MyApp::Article->retrieve_previous( $criteria,
                                     { order_by => 'created_on' } );

MyApp::Article->default_search_attributes(
                                     { order_by => 'created_on' } );

# Automatic joins for search and count

MyApp::CD->has_many(tracks => 'MyApp::Track');
MyApp::CD->has_many(tags => 'MyApp::Tag');
MyApp::CD->has_a(artist => 'MyApp::Artist');
MyApp::CD->might_have(liner_notes
    => 'MyApp::LinerNotes' => qw/notes/);

MyApp::Artist->search({ 'cds.year' => $cd }, # $cd->year subtituted
                              { order_by => 'artistid DESC' });

my ($tag) = $cd->tags; # Grab first tag off CD

my ($next) = $cd->retrieve_next( { 'tags.tag' => $tag },
                                   { order_by => 'title' } );

MyApp::CD->search( { 'liner_notes.notes' => { "!=", undef } } );

MyApp::CD->count(
       { 'year' => { '>', 1998 }, 'tags.tag' => 'Cheesy',
           'liner_notes.notes' => { 'like' => 'Buy%' } } );

# Multi-step joins

MyApp::Artist->search({ 'cds.tags.tag' => 'Shiny' });

# Retrieval with pre-loading

my ($cd) = MyApp::CD->search( { ... },
                   { prefetch => [ qw/artist liner_notes/ ] } );

$cd->artist # Pre-loaded

# Caching of resultsets (*experimental*)

__PACKAGE__->default_search_attributes( { use_resultset_cache => 1 } );

DESCRIPTION

Class::DBI::Sweet��壚梢��í��count���search���page���cache罘���純��sweet��ŝ�������宴�若�吾�����箴������障����� Class::DBI::Sweet��壔����������勖����純��箴水�í�у�合�������ʋ�号����˨�����Class::DBI��̥輝��������障�����

RETRIEVING OBJECTS

�����⓾�勖��膣≪�<�純��������������荅�箴≦�堺�����絮���с�����緇������������������с����障����� 荅�箴≦�堺�������壚��綽�荀���ŝ����í�<�若�帥�с�����

criteria

criteria���hash��障�����hashref��障�����arrayref��勉�í�������с����������������с����障����� SQL::Abstract���where��<�純����������������ŝ����激�с�潟��荐㊤�������障����� ��������ゃ�˩�������ŝ����吾�с�壔�������˨����с�������勉�с�������違�������若�帥����若�鴻�˨�壔����ŝ�若���������������� deflate�����障�����

attributes

case, cmp, convert, and logic

�����������勐����с��SQL::Abstact���constuctor���羝<��������荅�箴≦�堺����勖�壔�����������紊������障�����

{ cmp => 'like' }
order_by

Specifies the sort order of the results.

罎�膣∝�������勌研��恰�帥��������絎������障�����

{ order_by => 'created_on DESC' }
rows

罎�膣∝��������ROW��勖��紊ф�違�����絎������障����� �憜�������泣����若����������⓾�����RDBMs���Interbase���MaxDB���MySQL���PostgreSQL���������SQLite��с����� 篁����RDBMs��˨�ゃ����⓾�壔�������ャ�㋘�若�������������с�����������

{ rows => 10 }
offset

罎�膣∝�������勖��������ROW��勉�ŝ����祉�����������絎������障����� ���������筝���劫����ŝ�����0�����������í�˨����������障�����

{ offset => 0 }
page

page��<�純�������х憜����勉����若�吾�����絎������障����� ���������筝���劫����ŝ�����1�����������í�˨����������障�����

{ page => 1 }
prefetch

prefetch��<�純����������勉�ŝ�㋘�若�激�с�潟��listref��ф��絎������障����� �����������˨�����has_a��障�����might_haves��障�����Sweet��������í�若����������ŝ�������違�ŝ����障�������� ��∫�c�������ŝ����吾�с�壔�����菴������冴�˨����劫�ャ�˨����若�帥����若�鴻��紮������������������◐�<����̹�∫�c����������若����˨����� ��ゃ�ŝ�����������Sweet��壔�������с����������� ��違�ŝ�������若����˨�勐����⓾��'Essential'��˨�í�����罎�膣≪�������障�����

{ prefetch => [ qw/some_rel some_other_rel/ ] }

Sweet��勖�������壠�������с�壔�������������勉�˨�í�������勐����˨�� N���1��у����障����˨�⓾�潟�帥�с�����������������'sweet__N_'���臀勉����� ��˨�í����������ゃ�ŝ�≪�鴻�˨����c�☎����������障����� �����ŝ����勉�≪����ŝ�宴�若�激�с�潟�ч�激����������勉�˨�í�����篏睡����������勉�с�������違�� �����ŝ�����篏睡�������������若�帥����若�鴻����˨�í����勰�激����̹����������ŝ�����羈���������⓾�������������� 鐚�箴�������Oracle��с�姒��鐚������������с��鐚� Sweet��壠����ŝ��������鐚������勛�劫�ャ�ʋ��絖���������ŝ����勉�˨�í�������̬申��������������若�帥����若�鴻�勉����í�若�� ��榊�������������������������障��������

use_resultset_cache

罎�膣∝�������勉�㏍�c����激�ャ���櫝�純�˨����障����� ��������壠�����絎�薑������с�������������☗ぇ荀閞<��gotchas��壚����������勖侊�����у�演�冴�ɱ�掩�����莎激�������������������障�������� ��������������������壔����ŝ����壔�����篏������������������������障�����

�����壔�ɾ�������勛�冴�˨�������ŝ�������������í�若����潟�鴻�勐�馹����篏�薑������������������ resultset��奝�御�������˩戎��������������鴻����с����������� �����ŝ����勉�≪����ŝ�宴�若�激�с�潟�˨����������������������壔����ŝ�若�˨�������⓾���������櫝�純�˨����������� ��㏍�c����激�ャ�ŝ�若����若����������������˨����若�帥����若�鴻����������若�帥�������c�⓾�������������� ��ц�純�勌��筝����綣����莎激�������с�����������

profile_cache

��í�勉�������ŝ�㏍�若�̹�≪����⓾�㏍�c����激�ャ��������/��㏍�c����激�ャ����鴻�������c�������� ->profiling_data��˨����c�☀����蚊����障����� �����������ŝ�����Sweet��˨����������違�㏍�若����˨�с����c�〠⓲��������ŝ����勉�с�������違�� �����������壔�í�鴻�<�帥����若�帥�с�������勉��羈���������⓾��������������

__PACKAGE__->profiling_data({ });

��í�<�������壔�������ŝ�����Base��壔�í�鴻�������ŝ����勉����若����˨�壔�í�鴻�勉��絅純�帥�˨�������障�����

disable_sql_paging

���������荐㊤�������������ŝ����違����若�吾�潟�違��SQL��鴻����若����<�潟�����篏睡����������������壔�с����障����� ->search��<�純�������勛�����������slicing��ゃ����㋘�若�帥�˨�����Sweet��˨����若�吾�潟�違��荀�膺���������障�� 鐚����絽吾��緇���������ŝ�<�˨����冴����������⓾��������篏睡�������������������с��鐚� resultset��㏍�c����激�ャ��篏睡����������⓾����������˨�������鴻����������������障����壠��篏���勉�壔����ŝ�����������罎�膣≪����������勉� 綵鴻�̥����<�障��

count

荅�箴≦�堺����˩����眼�����Row��勖�違�勐����違��菴���眼����障����� count��<�純��������offset���order_by���and rows��������⓾�障�����

$count = MyApp::Article->count(%criteria);

��鴻�˨�í�若�潟�潟����㏍�鴻����勉�ゃ����㋘�若�帥�障����壔�ŝ�鴻����潟�潟����㏍�鴻����勉�ŝ����吾�с�壔����勰��������菴���眼����障�����

@objects  = MyApp::Article->search(%criteria);

$iterator = MyApp::Article->search(%criteria);

search_like

{ cmp => 'like' }絮���с��菴遵�������〠��膣≪����障�����

page

�����若�吾�ŝ����吾�с�壔�������ゃ����㋘�若�帥��菴���眼����障����� �����若�吾�ŝ����吾�с�壔�����Data::Page��勉�ゃ�潟�鴻�帥�潟�鴻�ŝ����吾�с�壔����с�����

( $page, $iterator )
    = MyApp::Article->page( $criteria, { rows => 10, page => 2 );

printf( "Results %d - %d of %d Found\n",
    $page->first, $page->last, $page->total_entries );

pager

page��<�純�������勉����ゃ�ŝ�≪�鴻�с�����

retrieve_all

Class::DBI���������attributes��勐����違����������с��attributes��壔�������激�ャ���������� ��������激�ャ�ŝ����<�㋘�潟�鴻�с����������������с����障�����

$iterator = MyApp::Article->retrieve_all( order_by => 'created_on' );

retrieve_next

order_by絮���э����障����壔�����order_by������絎������ŝ����勉�с�������違����í�ゃ����ŝ�㏍�種����ц��箴≦�堺����������������ŝ����� �憜����勉�㋘�潟�若�����荐���蚊����〠❶��勉�㋘�潟�若�����菴���眼����障�� ��������壔�ŝ����吾�с�壔����<�純�������������☗�若�喝�冴����ŝ�������違�ŝ����障��������

retrieve_previous

retrieve_next��<�純����������������������ŝ����勉�с��������retrieve_previous��<�純�������壠����勉�㋘�潟�若�����罎�膣≪����障��

CACHING OBJECTS

��ŝ����吾�с�壔����壔�㏍�c����激�ャ��deflate��������倶����т��絖������������с����������� Primary���Essential��˨�í�������������㏍�c����激�ャ�������障�����

cache

��壔�í�鴻�<�純�������с��鐚����������荐㊤�������������勉�с�������違�㏍�c����激�ャ���櫝�純����ŝ����障����� ��í�勉�㏍�c����激�ャ�ŝ����吾�с�壔����с��get���set���remove��<�純����������泣����若����������障�����

__PACKAGE__->cache(
    Cache::FastMmap->new(
        share_file => '/tmp/cdbi',
        expire_time => 3600
    )
);

cache_key

��壔�í�鴻�������í�ゃ����ŝ�㏍�若�����罕���������������ŝ����吾�с�壔����˨�㏍�c����激�ャ�㏍�若��菴���眼����障�����

Overloaded methods

_init

Class::DBI��勐�������㏍�c����激�ャ����ŝ�若����若�í�ゃ�������⓾����障����� ��㏍�c����激�ャ����������������翫�������㏍�c����激�ャ�ŝ����吾�с�壔�����菴���眼�������障����� ��㏍�c����激�ャ����鴻�������翫�������違�������ŝ����吾�с�壔�����篏���������������㏍�c����激�ャ�˩��絖���������障�����

create

�����勉����若����˨�勛�冴�勐����⓾�勉�㏍�c����激�ャ���𡉴�������ゆ�㏍����������翫�����罨<�勖��膣≪�勖����鴻�ч����㏍�c����激�ャ�������障�����

retrieve

��㏍�c����激�ャ����������˨�������ŝ����吾�с�壔�����select�����ŝ�㋘�若�˨�����inflate������罨<��綵合����⓾����������с�����������

update

��ŝ����吾�с�壔����壔�㏍�c����激�ャ����������ゃ��������罨<�勖��膣∽��������綺⓾�㏍�c����激�ャ�������障�����

delete

��ŝ����吾�с�壔����壔�㏍�c����激�ャ����������ゃ�������障�����

UNIVERSALLY UNIQUE IDENTIFIERS

������UUID���絖������������í�ゃ����ŝ�勉�˨�í����˩���������������������櫝�純�˨��������CHAR(36)��˨�í����� ��主�����絋ュ����с�����

__PACKAGE__->sequence('uuid');

AUTHORS

Christian Hansen <ch@ngmedia.com>

Matt S Trout <mstrout@cpan.org>

Andy Grundman <andy@hybridized.org>

THANKS TO

Danijel Milicevic, Jesse Sheidlower, Marcus Ramberg, Sebastian Riedel, Viljo Marrandi

SUPPORT

#catalyst on irc://irc.perl.org

http://lists.rawmode.org/mailman/listinfo/catalyst

http://lists.rawmode.org/mailman/listinfo/catalyst-dev

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Class::DBI

Data::Page

Data::UUID

SQL::Abstract

Catalyst

http://cpan.robm.fastmail.fm/cache_perf.html A comparison of different caching modules for perl.

膺肢┳���

atsushi kobayashi(nekokak@users.sourceforge.jp)