DBIC

DBIC������\���\������¼\·\���\��я��̢����¼���������»��������Ï����ď����я���������������㏢���������쏢�¹�������̏������������������������������������������������������Ώ�����

����DBIC������\���\������¼\·\���\��я��������«��¡񏪳���²���»��������Ï����ď����я���������������㏢����������»��С���ӏ��̏������������µ��������¡ߏ�����»�ߏ����������������������������������������Ώ��������·�����ď�¡򡭏������ޏ������������������·��������¡�entry\������¼\���\���������comment\������¼\���\���������¹�����𡯏����������Ώ��������·�����������Ï�¡�

mysql> desc entry;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name    | varchar(255) | NO   |     | NULL    |                | 
| content | text         | YES  |     | NULL    |                | 
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)


mysql> desc comment; 
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment | 
| entry_id | int(11)      | NO   |     | NULL    |                | 
| name     | varchar(255) | NO   |     | NULL    |                | 
| content  | text         | YES  |     | NULL    |                | 
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Entry has_many Comment ( Comment belongs_to Entry ) �����������·������������¹��������¡��³������¾���¹���������\���\������¼\·\���\��я������돪�²¼���������������Ï��������������������������¹��¡�

# MyApp/Schema/Entry.pm
__PACKAGE__->has_many(
    comments => 'MyApp::Schema::Comment', 
    'entry_id'
);
 
# MyApp/Schema/Comment.pm
__PACKAGE__->belongs_to( entry_id => 'MyApp::Schema::Entry' );

���µ��������¡�������������������������������돪㏢�«���������«���������������­���������¹��¡����������돢¡񏪴�̏��«���������������¹���������¹�����������ď�������¡�has_many ������ belongs_to \���\½\���\��������ҏ����������я�����������������\���\���\¹����¾����������������������� __PACKAGE__ �����������³������������ ����������������½Ð���·�����Ï��� ��¡�has_many ������ belongs_to �����¡돫⏢ď�ԏ���������������½��Џ��«������������������������\���\���\¹������ ����������������½Ð���µ�����������Ï��� ���������������Ð���»���������������������µ��������¡�

�����������돢¡�돫������ӡ���Ï����䏪���쏢����\������¼\������쏢�쏢�½���������������������������쏢��

__PACKAGE__->method( 
    'accessor_name' => 'MyApp::Schema::Hoge', 
    'FOREIGN KEY' or \%cond,
    \%attrs,
);

1. �������돫��돫⏢ď�ԏ����������� accessor_name ������»������������¹�������������³�������¾����돢���� prefetch ���·�����ď�������¡񏪴��ӏ����������������������� DBIx::Class::ResultSet\���\���\���\��\���\������������³�������¾����돢����\���\½\���\������̏�������𡯏����ď��������¹������
2. �����������ѡ돫⏢ď�ԏ����������� ������������½Ð���µ�����������Ï�����»������������¹����������
3. ������»��돫⏢ď�ԏ����������� JOIN �����������̏����Џ��µ������������ FOREIGN KEY ������»������������¹����������\���\���\·\���\���\���\���\���\���\¹������ JOIN ������¾���·���������»������������¹���������³������������²������½
����������»��돫⏢ď�ԏ��̏��µ������ or undef ������¾���¹�����������������¡�accessor_name ���� FOREIGN KEY ���������«���������µ������������ JOIN ����
4. ������»����돫⏢ď�ԏ��������������½�������¾������¾���·������� join_type ����������������������»������������¹����������������������¾���·������̏��µ���ޏ��������о�����¢̏�¡��������·������������ undef �����ҏ��������¹

�돪�¾������쏢���ď����������ď��³���������������ޏ��쏢��\���\���\���\������������������������������� ���� FOREIGN KEY ������������������\������¼\���\�������������������\«\���\����������������« ��¡ߏ��������������������������������·�����������Ï��«���폪��������������²��ҏ�������������¡�³�돪���ԏ�����\������¼\��\���������\������¼\��ď�����»�������������������\«\���\�������Comment\������¼\���\���������entry_id�����Ώ����ď������������������� FOREIGN KEY �����������я��������¹����¹������������������������¹���Ï��Ï�������������¡񏪤��ď���̏�¡�(���­Ž\��Ï��Ž\����)

has_many ������¾���¹���������¹�����𡯏����������Ώ�����

�����������돢¡�has_many ������\���\������¼\·\���\��я�����¾���¹�����¡�Catalyst������»���������¾���¹��������ҏ������������·������¹�����𡯏����������Ώ��������¹��¡�

# Controller
$c->model('DBIC::Entry')->search(
    {},
    {   
        # ���³���³������»������������¹����������������\������¼\���\������¾������������������������������\���\������¼\·\���\��я������¾�����(has_many���������«belongs_to���������«�����������돫��돫⏢ď��)�����������������������돪؏��쏢��
        prefetch => [ qw/comments/ ], 
    },
);

# Schema ( Schema/Entry.pm )
# ---------------------------
# \���\��ď��¼\���1
# ---------------------------
 
# ������������½Ð���·�����Ï����� PRIMARY KEY ��������¡񏢱���������½Ð���µ�����������Ï����� FOREIGN_KEY ������������ LEFT JOIN
# LEFT JOIN entry.id = comment.entry_id
 
__PACKAGE__->has_many(
    comemnts => 'MyApp::Schema::Comment',
    'entry_id',
);
  
 
# ---------------------------
# \���\��ď��¼\���2
# ---------------------------
 
# ������������½Ð���·�����Ï����� PRIMARY KEY ��������¡񏢱���������½Ð���µ�����������Ï����� FOREIGN KEY ������������ INNER JOIN
# INNER JOIN entry.id = comment.entry_id
 
__PACKAGE__->has_many(
    comments => 'MyApp::Schema::Comment',
    'entry_id',
    { join_type => 'inner' }, # join_type ����������̏����돪����»����돫⏢ď�ԏ�����
);
  
 
# ---------------------------
# \���\��ď��¼\���3
# ---------------------------
 
# ������������½Ð���·�����Ï���������돪؏�����\«\���\�����������¡񏢱���������½Ð���µ�����������Ï����� FOREIGN KEY ������������ LEFT JOIN
# LEFT JOIN entry.name = comment.entry_id
 
__PACKAGE__->has_many(
    comments => 'MyApp::Schema::Comment',
    { 'foreign.entry_id' => 'self.name' },
);

\���\��ď��¼\���1������\��쏢�¼\½\���\���\���\¹�������������������¹��¡�\���\��ď��¼\���2���������������Ï����� LEFT JOIN ������ INNER JOIN ������������¹¹���¹���������³������������²������½���������¹��¡�\���\���\���\���\���������������������������½Ð���·�����Ï����� PRIMARY KEY ���� JOIN ������»������������������������¹���̏�¡�\���\��ď��¼\���3���������������Ï��������¾������\«\���\��������ҏ����̏����Џ��¹���������³������������������������²������½���������¹��¡�

has_one, might_have ������¾���¹���������¹�����𡯏����������Ώ�����

\���\���\���\���\µ\�������������¾�����¢̏�¡�
has_many ��������������������ޏ��������������¹��¡�has_many ���� 1���Ð������ �����������·����������������������Ð���·��������¡��³������������������ 1���Ð1 �����������·������������¹��¡�has_one ������ might_have �����¡돫�������������\���\���\���\���\��������� JOIN ���� INNER JOIN ���« LEFT JOIN ���«��������������������������������¡�

belongs_to ������¾���¹���������¹�����𡯏����������Ώ�����

¼���������¡�belongs_to ������¾���¹���������¹�����𡯏����������Ώ��������¹��¡��³������������ Catalyst ������»���������������¾���¹��������������������¹��¡�

# Controller
$c->model('DBIC::Comment')->search(
    {},
    {   
        # ���³���³������»������������¹����������������\������¼\���\������¾������������������������������\���\������¼\·\���\��я������¾�����(has_many���������«belongs_to���������«�����������돫��돫⏢ď��)�����������������������돪؏��쏢��
        prefetch => [ qw/entry_id/ ], 
    },
);

# Schema ( Schema/Comment.pm )
# ---------------------------
# \���\��ď��¼\���1
# ---------------------------

# ������»��돫⏢ď�ԏ��̏��µ������������������¡�accessor_name(entry_id)���� JOIN ������¾���·������������·�����������̏����Џ��µ���������������¹
# ������������½Ð���·�����Ï����� FOREIGN KEY ��������¡񏢱���������½Ð���µ�����������Ï����� PRIMARY KEY ������������ INNER JOIN
# JOIN ON comment.entry_id = entry.id

__PACKAGE__->belongs_to( entry_id => 'MyApp::Schema::Entry' );
 

# ---------------------------
# \���\��ď��¼\���2
# ---------------------------

# ������������½Ð���·�����Ï����� FOREIGN KEY ��������¡񏢱���������½Ð���µ�����������Ï����� PRIMARY KEY ������������ RIGHT JOIN
# RIGHT JOIN ON comment.entry_id = entry.id

__PACKAGE__->belongs_to( 
    entry_id => 'MyApp::Schema::Entry', 
    undef, 
    { join_type => 'right' } ); # join_type ����������̏����돪����»����돫⏢ď��
 

# ---------------------------
# \���\��ď��¼\���3
# ---------------------------

# ������������½Ð���·�����Ï����� FOREIGN KEY ��������¡񏢱���������½Ð���µ�����������Ï����� name ������������ LEFT JOIN
# JOIN ON comment.entry_id = entry.name

__PACKAGE__->has_many(
    comments => 'MyApp::Schema::Entry',
    { 'foreign.name' => 'self.entry_id' },

\���\��ď��¼\���1������\��쏢�¼\½\���\���\���\¹�������������������¹��¡�\���\��ď��¼\���2���������������Ï����� INNER JOIN ������ RIGHT JOIN ������������¹¹���¹���������³������������²������½���������¹��¡�\���\���\���\���\���������������������������½Ð���·�����Ï����� PRIMARY KEY ���� JOIN ������»������������������������¹���̏�¡�\���\��ď��¼\���3���������������Ï��������¾������\«\���\��������ҏ����̏����Џ��¹���������³������������������������²������½���������¹��¡�

many_to_many ������¾���¹���������¹�����𡯏����������Ώ�����

���¹���������������»�����я�¡�¡�»��������������ď��³���������µ���������������¹��¡�¡��������·���������������«��µ��𡯏��������������������µ��������������

����»²¹�������
������������������¾��䏢�·���������������������ď������������������³�����������������ҏ��������¼���¾��¡���������DBIx::Class::Relationship - Inter-table relationships

resultset_attributes �����ҏ����̏����Џ��·��������¡�\������¼\���\��������� alias ������ me ���«������������¹¹���¹������ with DBIC

���³�����я��Ð�����я�������¡��µ���µ�����ď��������������¹��¡�¡�(oŽ��Ώ�Ï��Ž���o)Ž���Ž���

���򏩡��䏢����DBIC�������������������¹��¡�돪������perl������O/R\���\���\��Џ��¼��������������DBIC������»������������������������������¹��¡򡭏������ޏ�����SQL�����������������������������������������̏����������я��������¹���̏�¡񏪴��»��������SQL��������������������������¡�¡�¡��¹���돢����\¹\«\������¼\���\���\���\���\���\¹���������������������������·���������������������¹�������������������폢����������

���µ��������¡�\������¼\���\��������������� delete_flag �����ҏ���̏����돪���ӏ����������������Ï�����������·�ߏ��·��������������������¡�find���������«search���·�����ď��������­������ delete_flag = 0 ������\���\���\���\���\���������������¾���·������������·�����������̏����Џ��¹�����������ď�������¡�Schema\���\���\���\������������³���������������Ï����������Ώ�����������½��Џ��������������������������·�����ď�¡�

__PACKAGE__->resultset_attributes({ where => {delete_flag => 0} });

�����ď��������³��������¡�join������prefetch���·�����ď��������­���������������� delete_flag ������»������������̏��򏪳������������¹��¡ߏ�����������������\��\������¼����½Ð���������·���������������������¹��¡�join���·������\������¼\���\��������������� delete_flag ���̏������������ď��������������¹��¡��³������������²��я����ҏ��¹�����������ď�������������������¡񏪢��������叢�������������Ώ�����������

__PACKAGE__->resultset_attributes({ where => {'me.delete_flag' => 0} });

���³���������������Ï�������¡��ӏ�����������\������¼\���\��������� delete_flag �������������«���폢¡ߏ����ҏ�����¼�����·�����������²���������ÐOK���������¹��¡����ď�������¡��³����������������������������������Џ��­���̏����������������»�����я��Ï��Ï�¡��³������\������¼\���\���������\���\���\��я�����»��������Ï��������������������������������¹���̏�¡񏪴���������\������¼\���\������«���������³������\������¼\���\��������ҏ����������я�����¾���¹������������µ���·��������������¾���·�������\������¼\������µ���������������»�����я��·��¡�¡�
�������Ï��Ï�����»�ߏ����������������ď����я��������¹���̏�¡��½���������½������join���������«prefetch�������������­��������������¡�join���µ�����������������Ï�����resultset_attributes������»������������·������\���\���\���\���\���������������¾���·������������������Ð���������������������폢�폢��

����ߏ�������¡����������я�����¾���¹���������������\������¼\���\������¾������ alias ���̏���ޏ��������������������������������������������������Ð�����������������я��������¹����

DBIC_TRACE������SQL������½Ð�������¹���������Ð���������«���������³���������������¹���̏�¡�DBIC��������������������������¡�\������¼\���\���������alias������ me �����������������Ï��������������������������������������������¹��¡�½Ð�������µ������������SQL���������³�����я�����������������¡�

SELECT me.id, me.comment, me.created_at, me.updated_at FROM comments me WHERE ( ( ( ( ( ( me.id = ? ) ) ) ) AND ( delete_flag = ? ) ) ): '1', '0'

���³������ alias ���������������ҏ�����¹¹���¹�����������򏪳������ҏ���������㏢���������Ώ��������·�����ď�¡�(=Ž��Ώ�Ï��Ž���)Ž�������Schema\���\���\���\������������³���������������Ï�����»��������������ҏ��·�����������²���������Ð�������µ���²��¡�

__PACKAGE__->resultset_attributes({
    alias => 'comment',                   # From������돪�³�돢����alias
    from  => [{ comment => 'comments' }], # From�����������alias (alias => table���¾)
    where => { 'comment.delete_flag' => 0 },
});

���³������¾��؏����ӏ����������������叢��������������ޏ����������������Ï�����SQL������½Ð�������·�����������Ώ�������������¡�

SELECT comment.id, comment.comment, comment.created_at, comment.updated_at FROM comments comment WHERE ( ( ( ( ( ( comment.id = ? ) ) ) ) AND ( comment.delete_flag = ? ) ) ): '1', '0'

½Ð���������������·�����ď�����¡��³������������������������������ alias ���������·������ comment ����»���������������������������������¡�돪���ġ����·������ 'comment.delete_flag' => 0 ������½��Џ��ޏ��������¹��������¡돢¡돢���������·���������������³��������������������³����������������돫��������Ï���

\·\��\���\¹\���\���\��\���������Catalyst������DB\¹\­���¼\��������ҏ�돫�����

Catalyst������Model������������DBIC������»��������Ï�������\���\���\���\���\������������¹����������������
���Ï��Ï����������������Ï��������ޏ�������¡񏪡���������Catalyst::Model::DBIC::Schema������»���������������DB\¹\­���¼\��������ҏ�돫����������������Ώ��������·�����ď�¡����������������Ï��«������½�̏��������¹��������¡�(*���­��\����*)

���½���������½��������¡�Catalyst::Model::DBIC::Schema������»���������������DB\¹\­���¼\��������ҏ�돫�������������������¡��돪�²¼������3���������������򏪳������̏����������½�����Ï��������¹��¡����؏����������؏�������¡�

1. ����叢������������돩Ώ��¹������Schema\���\���\¹������»���������
2. Schema::Loader����������只����������DB���«������Schema\���\���\¹�����ҏ�����������
3. Schema::Loader����������只����������DB���«������Schema::Loader\���\���\¹�����ҏ�����������

����²��я�����2�����ӏ��������򏪳�������»��������Ï��³���������������·���������·�����ď�¡�3�����ӏ����������������������ď����я��������¹���ޏ�������¡񏪳���²��я����������Ώ��������������������������������������«����������������»�ߏ����������ď�����������¡�

¼�����돪������������������\���\���\���\���\������¼���������������ޏ����������я��������¹��������¡򡭏������ޡ��������ޏ�¡�

./script/myapp_create.pl model DBIC DBIC::Schema MyApp::Schema create=static dbi:mysql:[db_name] [user] [pass]

��������������¡��³�����я��������¹������������³Ð��𡯏�����������������������...orz�������������������Ï��������ޏ�������¡�\·\��\���\¹\���\���\��\������������·�����������Ώ��������·�����ď�����(Ž���Ž\��Ï��Ž\)Ž���Ž���

make_schema.sh
#!/bin/sh

if [ $# -eq 3 ]; then
    app_name=`echo $1 | tr "[:upper:]" "[:lower:]"`
    command="../script/${app_name}_create.pl model DBIC DBIC::Schema ${1}::Schema create=static dbi:mysql:${app_name} $2 $3"
    echo $command

    echo -n "¼���¹��ԏ��·���������¹��¡��������������·���������������¹���«����[Yn]: "
    read confirm

    case $confirm in
        y|Y)    $command
                ;;
        *)      exit
                ;;
    esac
else
    echo 'Usage: ./make_schema.sh MyApp user pass'
fi

DB�������¾����돢������¡�\��\���\���\��\���\������¾����������¾������»��⏢�������·�����ď������������������������������³���������̏���돪���я��������¹��¡�

./make_schema.sh MyApp sasata299 secret

�����Ώ����ď���������������������������¼���¹��ԏ��·���������¹��¡��³������������¼���¹��ԏ��¹������������\���\���\���\���\������¼������\³\���\���\������̏��½¼�����µ��������������¡�¼���¹��ԏ��¹���������«������³���������·���������������������������¹��¡��³������������������������������������������³��⏢�������������������������·������(Ž��Ρ��Ž���)Ž��Ž���Ž��Ž���Ž��

����¡����������������Ώ�������¡�\·\��\���\¹\���\���\��\���������if¾���·�����������¡�!=���������«-gt���������«-eq���������«������»�������������̏��ޏ����ҏ��򏩡��䏪�����������������·�����ď�¡�
=���������«!=������������»��⏪�я�����³��я�¡�-gt���������«-et��������ď�ԏ�����������³��я����������я��������¹��������¡򏪪����������������«�����������ď�¡�¡�

---
����������������������������������㏢���������Ώ�����������²��ҏ��̏��¼�����������������ď���������¼«����̏����Џ�����\���\�����¡�

�����������돢¡�Catalyst���������̏��̏��¼������������DB\¹\­���¼\��������ҏ�돫��������쏢��(\������\���­)\����������������������­������������2\���\��ď��¼\��я����������������¹��¡�

­��� DB\¹\­���¼\���������¼�������돢������돫�����
��������DBIx::Class::Schema������·���¾µ���·������Schema\���\���\¹���̏�����������µ���������������¹��¡�
��������³���\������¼\���\���������\¹\­���¼\�����������돫��������������������¹��¡�

­��DB\¹\­���¼\���������µ��������»��Џ�����¼«����돪����������
��������DBIx::Class::Schema::Loader������·���¾µ���·������Schema::Loader\���\���\¹���̏�돫�����µ���������������¹��¡�
��������³���\������¼\���\���������\¹\­���¼\�����������돫��������������������»�����я�¡�

���³���������������ޏ��������¹��¡�µ­»��ӏ�����������­�������»������������������·�����ď�¡�­������»��������������ď��«�����������ď�����­��������������­������\���\���\���\���\������¼������"static"���������·�����������������ď��������³������������"dynamic"���������¹���������������ޏ��������¹��¡�

�����������á돫�����������

DBIC»�����������������������������¡��¹������������DB\¹\­���¼\���������»����������������������������쏢��(���­Ž\��Ï��Ž\����)�����������������������­���������������³�����я�����������������¡�DBIC�����Џ�������돫�����������DB\¹\­���¼\������̏������������������돢�������¹���̏�¡��������·���������ޏ��������Ð���³�����я���������������������\���\���\���\���\������¼��������돫��������������¹��¡�

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./ -e 'make_schema_at "MyApp::Schema", {debug => 1}, ["dbi:mysql:myapp","sasata299","secret"]'

���³���������������ޏ�������¡�¼���¹��ԏ��·������\���\��\���\���\���\���������MyApp\���\���\���\�������½Ð��������������¡��½����������������DBIx::Class::Schema������·���¾µ���·������Schema\���\���\¹���̏�����������µ���������������������������¹��¡���������������¡�

./script/myapp_create.pl model DBIC DBIC::Schema MyApp::Schema dbi:mysql:myapp sasata299 secret

���������«���¹���������ÐOK��¡��³������������\¹\­���¼\�����돫�����¼��Џ�����������������²��ҏ��������­�����ď����я��������������������������«��������¡�돫������������������ď�������µ��𡯏��������������������µ������(>_<)

�����������������Ώ�������¡�DBIC������»���������¾���¹�������Catalyst������»���������������������¾���¹�����������������������돪���쏢����DB\¹\­���¼\��������ҏ�����������¹��Џ��������³����������½Ð�����������������Ώ����ď��������������¹��¡���������������
karaage299 at gmail.com
��돪������������µ­»���
\���\��\���\���\���\���