DBIx::Customのドキュメント
DBIx::Customは、PerlのO/Rマッパーです。O/Rマッパーとは、データベースへのアクセスを簡単にしてくれるモジュールのことを言います。
DBIx::Customは、生のDBIでの作業効率に辛さを感じたり、抽象度が高いO/Rマッパーよりも、シンプルなO/Rマッパーを好む場合に、検討できる選択肢のひとつです。
DBIx::Customの特徴
DBIx::Customの特徴です。
- insert, update, delete, selectを簡単実行
- 複数データの一括insert、RDBMS対応の場合はバルクインサート可能
- SQLに対する薄いレイヤーを提供。純粋なSQLを使いたくなったときもOK。
- 検索が柔軟にできるwhere句、名前付きプレースホルダー、モデルのサポート
- join句を定義して、外部キーで、テーブルを連結。order by句、limit句など自由に構文を書ける
- トランザクションをサポート。接続管理あり、プリフォークサーバー上でも正しく動く
- MySQL,MariaDB,PostgreSQL,SQLite,Oracle,SQL Server,Microsoft Access対応
- 2018年時の廃止予定機能を除いて、後方互換性を維持。
データベースへの接続
データベースに接続します。
use DBIx::Custom;
# SQLiteへ接続
my $dbi = DBIx::Custom->connect('dbi:SQLite:dbname=bookshop');
# MySQL,MariaDBへ接続(utf8mb4対応)
# 接続管理はデフォルトで行われプリフォークサーバーでも正しく動く
my $dbi = DBIx::Custom->connect(
'dbi:mysql:database=bookshop',
'ken',
'!LFKD%$&',
{mysql_enable_utf8mb4 => 1}
);
モデルの作成
モデルを作成します。
# モデルの作成
$dbi->model('book');
# モデルを作成join句を定義する場合
$dbi->model(
table => 'book',
join => [
'left join author on book.author_id = author.id',
'left join publisher on book.publisher_id = publisher.id'
]
);
insert
# insertで挿入
$dbi->model('book')->insert({id => 1, title => 'Perl'});
# insertで複数データの挿入
$dbi->model('book')->insert(
[
{id => 1, title => 'Perl'}
{id => 2, title => 'Mojolicious'}
]
);
# insertで複数データの挿入(バルクインサート)
$dbi->model('book')->insert(
[
{id => 1, title => 'Perl'}
{id => 2, title => 'Mojolicious'}
],
bulk_insert => 1
);
update
# uppdateで更新
$dbi->model('book')->update({title => 'Perl'}, where => {id => 1});
# deleteで削除
$dbi->model('book')->delete(where => {id => 1});
select
# selectで選択(すべてのフィールド)
my $result = $dbi->model('book')->select(where => {id => 1});
# selectで選択(フィールドを指定)
my $result = $dbi->model('book')->select(['id', 'title'], where => {id => 1});
# selectで選択(IDが1か2か3)
my $result = $dbi->model('book')->select(where => {id => [1, 2, 3]});
# selectで選択(タイトルにPerlを含み価格が2000円以下)
my $result = $dbi->model('book')->select(
where => "title like '%Perl%' and price <= 2000"
);
# selectで選択(タイトルにPerlを含み価格が2000円以下) 変数を使う場合
# 名前付きプレースホルダーを使用
my $title = 'Perl';
my $price = 2000;
my $result = $dbi->model('book')->select(
where => [
"title like :title and price <= :price",
{title => $title, price => $price}
]
);
# selectで選択(タイトルにPerlを含み価格が2000円以下)
# where句を動的に生成
my $title = 'Perl';
my $price = 2000;
my $result = $dbi->model('book')->select(
where => [
['and', "title like :title", "price <= :price"],
{title => $title, price => $price}
]
);
# selectで選択(タイトルにPerlを含み価格が2000円以下または5000円以上)
# where句を動的に生成して、orで、複数条件指定
my $title = 'Perl';
my $price1 = 2000;
my $price2 = 5000;
my $result = $dbi->model('book')->select(
where => [
['and', "title like :title", ['or', "price <= :price", "price >= :price"],
{title => $title, price => [$price1, $price2]}
]
);
# selectで選択(タイトルにPerlを含み価格が2000円以下または5000円以上)
# where句を動的に生成して、orで、複数条件指定
# 一つの価格は指定されなかった
my $title = 'Perl';
my $price2 = 5000;
my $result = $dbi->model('book')->select(
where => [
['and', "title like :title", ['or', "price <= :price", "price >= :price"],
{title => $title, price => [$dbi->not_exists, $price2]}
]
);
# selectで選択(タイトルにPerlを含み価格が2000円以下) 変数を使う場合
# 名前付きプレースホルダーのシンタックシュガー
my $title = 'Perl';
my $price = 2000;
my $result = $dbi->model('book')->select(
where => [
":title{like} and :price{<=}",
{title => $title, price => $price}
]
);
DBIx::Customの解説
DBIx::Customの解説記事です。
- インストール
- データベースへの接続
- モデルの作成
- select - 行の選択
- insert - 行の挿入
- update - 行の更新
- delete - 行の削除
- execute - SQLの実行
- where - 条件の作成
- 行データの取得
- その他の機能
DBIx::Custom