Git Product home page Git Product logo

pdbc-manager's Introduction

PdbcManager

Build Status Coverage Status MIT License

blessしたリファレンスでデータベースを操作する

INSTALL

cpanm https://github.com/duck8823/pdbc-manager/tarball/master

SYNOPSIS

use 5.019;

use Data::Dumper;

use Pdbc;

# モジュールを動的に生成
BEGIN {
	struct 'Hoge', [ 'id', 'name', 'flg' ];
}

# データベースへの接続
my $manager = Pdbc::connect('Pg', 'dbname=test;host=localhost', "postgres");
# テーブルの作成
$manager->create(Hoge->new('INTEGER', 'TEXT', 'BOOLEAN'))->execute();
# データの挿入
$manager->insert(Hoge->new(1, 'name_1', 1))->execute();
$manager->insert(Hoge->new(2, 'name_2', 0))->execute();
# データの取得(リスト)
my $rows = $manager->from(Hoge)->list();
for my $row ($rows) {
	say Dumper $row;
}
$manager->from(Hoge)->where( Pdbc::Where->new( 'name', 'name', LIKE ) )->list();
# データの取得(一意)
my $row = $manager->from(Hoge)->where( Pdbc::Where->new( 'id', 1, EQUAL ) )->single_result();
say Dumper $row;
# データの更新
$row->{flg} = 0;
$manager->update($row)->where( Pdbc::Where->new('id', $row->{id}, EQUAL) )->execute();
# データの削除
$manager->from(Hoge)->where( Pdbc::Where->new( 'id', 1, EQUAL ) )->delete()->execute();
# テーブルの削除
$manager->drop(Hoge)->execute();
# SQLの取得
my $create_sql = $manager->create(Hoge->new('INTEGER', 'TEXT', 'BOOLEAN'))->get_sql();
my $insert_sql = $manager->insert(Hoge->new(1, 'name_1', 1))->get_sql();
my $update_sql = $manager->update(Hoge->new(1, 'name_1', 0))->where( Pdbc::Where->new( 'id', 1, EQUAL ) )->get_sql();
my $delete_sql = $manager->from(Hoge)->where( Pdbc::Where->new( 'id', 1, EQUAL ) )->delete()->get_sql();
my $drop_sql = $manager->drop(Hoge)->get_sql();

USAGE

struct

Python の namedtuple に似た書き方で、クラスを自動生成します.

BEGIN {
    struct 'Hoge', [ 'id, 'name', 'flg' ];
}

は、以下のモジュールを自動的に生成します.

package Hoge;

use 5.019;
use feature 'signatures';
no warnings 'experimental::signatures';

sub new($pkg, $id, $name, $flg) {
    my $self = {
        id => $id,
        name => $name,
        flg => $flg
    };
    return bless $self, ref($pkg) || $pkg;
}
1;

第一引数がパッケージ名(テーブル名)、第二引数がキー(カラム名)一覧となります.

また、該当パッケージにサブルーチンを生成します.

sub Test {
	return bless [ 'id', 'name', 'flg' ], 'Hoge';
}

from や drop の引数にこのサブルーチンを渡すことで簡潔に記述できます.

Where

条件の組み合わせ

Pdbc::Where->new('column_1', 'value_1', EQUAL)
    ->and(Pdbc::Where->new('column_2', IS_NULL)
        ->or(Pdbc::Where->new('column_2', 'value_2', LIKE)))
    ->and(Pdbc::Where->new('column_3', 'value_3', EQUAL));

上記で生成されるSQL

WHERE ( column_1 = 'value_1' AND ( column_2 IS NULL OR column_2 LIKE '%value_2%' ) AND column_3 = 'value_3' )

Transaction

デフォルトはオートコミットですが、トランザクションを制御することもできます.

BEGIN TRANSACTION

$manager->begin;

COMMIT

$manager->commit;

ROLLBACK

$manager->rollback;

License

MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.