アーカイブ
Tyzohブログ - ikarashiさんのエントリ
 ikarashiさんのエントリ配信

2007/05/08
[Secure-SBM:006] データベース用のスキーマを作る

執筆者: ikarashi (8:32 pm)
こんにちは,五十嵐です.今回はデータベースにアクセスするための設定を作っていきます.

メインになる Users, Roles, Bookmarks, Tags の 4 つのテーブルの関係を整理しておきます.

左図は User と Role の関係,Bookmark と Role の関係を表したものです.
右図は User→Bookmark→Tag の関係を表したものです.
(開発初期に設計したものなので,実際の関係とは少しずれがありますが,大きくは変わっていません)

これらの関係図を見ながら,設定をしていきます.

Catalyst の Tutorial を参考にして,lib/SecureSBMDB.pm と lib/SecureSBMDB/ 下のファイルを作成していきます.lib/SecureSBMDB/ 下にはデータベースのテーブルにあわせて User.pm,Role.pm,Bookmark.pm,Tag.pm,UserRole.pm,UserTag.pm,BookmarkRole.pm,UserBookmark.pm,UserBookmarkTag.pm RoleOnwer.pm などのファイルを作成します.参考として,SecureSBMDB.pm と User.pm,UserRole.pm を載せます.

lib/SecureSBMDB.pm
     1  package SecureSBMDB;
     2      
     3  =head1 NAME 
     4
     5  SecureSBMDB - DBIC Schema Class
     6
     7  =cut
     8
     9  # Our schema needs to inherit from 'DBIx::Class::Schema'
    10  use base qw/DBIx::Class::Schema/;
    11
    12  # Need to load the DB Model classes here.
    13  # You can use this syntax if you want:
    14  #    __PACKAGE__->load_classes(qw/Book BookAuthor Author/);
    15  # Also, if you simply want to load all of the classes in a directory
    16  # of the same name as your schema class (as we do here) you can use:
    17  #    __PACKAGE__->load_classes(qw//);
    18  # But the variation below is more flexible in that it can be used to 
    19  # load from multiple namespaces.
    20  __PACKAGE__->load_classes({
    21          SecureSBMDB => [qw/
    22                  User
    23                  Role
    24                  Tag
    25                  Bookmark
    26                  UserRole
    27                  UserBookmark
    28                  UserBookmarkTag
    29                  UserBookmarkRole
    30                  RoleOwner
    31                  UserProfile
    32                  Information
    33                  Sideinfo
    34                  Adminstatus
    35          /]
    36  });
    37
    38  =head1 LISENCE
    39
    40  This Program is distributed under version 1.0 of the Rinza Public
    41  License Agreement, that is bundled with this package in the file
    42  LICENSE, and is available through the website at the following URL:
    43  http://www.tyzoh.jp/rinza/licenses/LICENSE-1.0.txt.
    44
    45  The Initial Developer of the Original Program is Nihon Unisys, Ltd.
    46  The Original Program is copyrighted (C) 2007 by Nihon Unisys, Ltd.
    47  with all rights reserved.
    48  There is NO WARRANTY OF ANY KIND by the Initial Developer of the
    49  Original Program.
    50
    51  =cut
    52
    53  1;

lib/SecureSBMDB/User.pm
     1  package SecureSBMDB::User;
     2
     3  use base qw/DBIx::Class/;  
     4
     5  # Load required DBIC stuff
     6  __PACKAGE__->load_components(qw/PK::Auto Core/);
     7  # Set the table name
     8  __PACKAGE__->table('users');
     9  # Set columns in table
    10  __PACKAGE__->add_columns(qw/
    11          user_id
    12          user_email
    13          user_password
    14          user_name
    15          user_surname
    16          /);
    17  # Set the primary key for the table
    18  __PACKAGE__->set_primary_key(qw/user_id/);
    19
    20  #
    21  # Set relationships:
    22  #
    23
    24  # has_many():
    25  #   args:
    26  #     1) Name of relationship, DBIC will create accessor with this name
    27  #     2) Name of the model class referenced by this relationship
    28  #     3) Column name in *foreign* table
    29  __PACKAGE__->has_many(user_roles => 'SecureSBMDB::UserRole', 
'user_id');
    30  __PACKAGE__->has_many(user_bookmarks =>  'SecureSBMDB::UserBookmark', 
'user_id');
    31  __PACKAGE__->has_many(role_owners => 'SecureSBMDB::RoleOwner', 
'user_id');
    32  __PACKAGE__->has_many(user_profiles =>  'SecureSBMDB::UserProfile',
'user_id');
    33
    34  # many_to_many():
    35  #   args:
    36  #     1) Name of relationship, DBIC will create accessor with this name
    37  #     2) Name of has_many() relationship this many_to_many() is 
shortcut for
    38  #     3) Name of belongs_to() relationship in model class of 
has_many() above 
    39  #   You must already have the has_many() defined to use a 
many_to_many().
    40  __PACKAGE__->many_to_many(roles => 'user_roles', 'role');
    41  __PACKAGE__->many_to_many(bookmarks => 'user_bookmarks', 
'bookmark');
    42  __PACKAGE__->many_to_many(own_roles => 'role_owners', 'role');
    43
    44
    45  =head1 NAME
    46
    47  SecureSBMDB::User - A model object representing a user.
    48
    49  =head1 DESCRIPTION
    50
    51  This is an object that represents a row in the 'users' table of 
your application
    52  database.  It uses DBIx::Class (aka, DBIC) to do ORM.
    53
    54  For Catalyst, this is designed to be used through 
MyApp::Model::SecureSBMDB.
    55  Offline utilities may wish to use this class directly.
    56
    57  =cut
    58
    59  =head1 LICENSE
    60
    61  This Program is distributed under version 1.0 of the Rinza Public
    62  License Agreement, that is bundled with this package in the file
    63  LICENSE, and is available through the website at the following URL:
    64  http://www.tyzoh.jp/rinza/licenses/LICENSE-1.0.txt.
    65
    66  The Initial Developer of the Original Program is Nihon Unisys, Ltd.
    67  The Original Program is copyrighted (C) 2007 by Nihon Unisys, Ltd.
    68  with all rights reserved.
    69  There is NO WARRANTY OF ANY KIND by the Initial Developer of the
    70  Original Program.
    71
    72  =cut
    73
    74  1;

lib/SecureSBMDB/UserRole.pm
     1  package SecureSBMDB::UserRole;
     2
     3  use base qw/DBIx::Class/;
     4
     5  # Load required DBIC stuff
     6  __PACKAGE__->load_components(qw/PK::Auto Core/);
     7  # Set the table name
     8  __PACKAGE__->table('user_roles');
     9  # Set columns in table
    10  __PACKAGE__->add_columns(qw/
    11          user_role_id
    12          user_id
    13          role_id
    14          /);
    15  # Set the primary key for the table
    16  __PACKAGE__->set_primary_key(qw/user_role_id/);
    17
    18  #
    19  # Set relationships:
    20  #
    21
    22  # belongs_to():
    23  #   args:
    24  #     1) Name of relationship, DBIC will create accessor with this name
    25  #     2) Name of the model class referenced by this relationship
    26  #     3) Column name in *this* table
    27  __PACKAGE__->belongs_to(user => 'SecureSBMDB::User', 'user_id');
    28  __PACKAGE__->belongs_to(role => 'SecureSBMDB::Role', 'role_id');
    29
    30  =head1 NAME
    31
    32  SecureSBMDB::UserRole - A model object representing the JOIN 
between a user and 
    33  a role.
    34
    35  =head1 DESCRIPTION
    36
    37  This is an object that represents a row in the 'user_roles' table 
of your 
    38  application database.  It uses DBIx::Class (aka, DBIC) to do ORM.
    39
    40  You probably won't need to use this class directly -- it will be 
automatically
    41  used by DBIC where joins are needed.
    42
    43  For Catalyst, this is designed to be used through 
MyApp::Model::SecureSBMDB.
    44  Offline utilities may wish to use this class directly.
    45
    46  =cut
    47
    48  =head1 LICENSE
    49
    50  This Program is distributed under version 1.0 of the Rinza Public
    51  License Agreement, that is bundled with this package in the file
    52  LICENSE, and is available through the website at the following URL:
    53  http://www.tyzoh.jp/rinza/licenses/LICENSE-1.0.txt.
    54
    55  The Initial Developer of the Original Program is Nihon Unisys, Ltd.
    56  The Original Program is copyrighted (C) 2007 by Nihon Unisys, Ltd.
    57  with all rights reserved.
    58  There is NO WARRANTY OF ANY KIND by the Initial Developer of the
    59  Original Program.
    60
    61  =cut
    62
    63  1;


has_many() は 1 対多の関係を記述します.これらのファイルを作成後,DBIC::Schema の設定を行います.

$ ./script/securesbm_create.pl model SecureSBMDB DBIC::Schema SecureSBMDB \
                     dbi:Pg:ssbmdb 'ssbm' 'ssbm' '{ AutoCommit => 1 }'
 exists "....(省略)/SecureSBM/script/../lib/SecureSBM/Model"
 exists "....(省略)/SecureSBM/script/../t/Model"
created "....(省略)/SecureSBM/script/../lib/SecureSBM/Model/SecureSBMDB.pm"
created "....(省略)/SecureSBM/script/../t/Model/SecureSBMDB.t"
$ 


ここで,ConfigLoader を使って,データベースの設定を YAML ファイルに設定することにします.以前のバージョンでは,catalyst.pl で自動的に .yml ファイルができたのですが... "securesbm.yml" ファイルを作成します.インデントは Tab コードではなく空白で行います(要注意).
---
name: SecureSBM

Model::SecureSBMDB:
  connect_info:
    - 'dbi:Pg:dbname=ssbmdb'
    - 'ssbm'
    - 'ssbm'
    - AutoCommit: 1


同時に,lib/SecureSBM/Model/SecureSBMDB.pm を変更します.設定ファイルを別にすることで,データベースを PostgreSQL 以外のものに変更することも簡単になります.
Index: lib/SecureSBM/Model/SecureSBMDB.pm
===================================================================
--- lib/SecureSBM/Model/SecureSBMDB.pm
+++ lib/SecureSBM/Model/SecureSBMDB.pm 
@@ -5,13 +5,6 @@
 
 __PACKAGE__->config(
     schema_class => 'SecureSBMDB',
-    connect_info => [
-        'dbi:Pg:ssbmdb',
-        'ssbm',
-        'ssbm',
-        { AutoCommit => 1 },
-        
-    ],
 );
 
 =head1 NAME


以上で,データベース関連の設定と Schma, Model の準備が一通りできました.この後は Controller と View の作成に入っていきます.

参考:

Keyword: Perl Catalyst Secure-SBM SSBM セキュア・ソーシャル・ブックマーク
ikarashiさんのブログを読む | コメント (0) | トラックバック数 (0) | 閲覧数 (3767)
Trackback is not accepted now.
印刷用ページ 友達に送る
 
投稿された内容の著作権はコメントの投稿者に帰属します。
サイト内検索
ブログ カレンダー
«  «  2008 3月  »  »
24 25 26 27 28 29 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5