The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Skinny - simple DBI wrapper/ORMapper

SYNOPSIS

create your db model base class.

    package Your::Model;
    use DBIx::Skinny setup => {
        dsn => 'dbi:SQLite:',
        username => '',
        password => '',
    };
    1;
    

create your db schema class. See DBIx::Skinny::Schema for docs on defining schema class.

    package Your::Model::Schema;
    use DBIx::Skinny::Schema;
    
    install_table user => schema {
        pk 'id';
        columns qw/
            id
            name
        /;
    };
    1;
    

in your execute script.

    use Your::Model;
    
    # insert new record.
    my $row = Your::Model->insert('user',
        {
            id   => 1,
        }
    );
    $row->update({name => 'nekokak'});

    $row = Your::Model->search_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
    $row->delete('user')

DESCRIPTION

DBIx::Skinny is simple DBI wrapper and simple O/R Mapper. Lightweight and Little dependence ORM. The Row objects is generated based on arbitrarily SQL.

METHOD

new

Arguments: $connection_info Return: DBIx::Skinny's instance object.

create your skinny instance. It is possible to use it even by the class method.

$connection_info is optional argment.

When $connection_info is specified, new method connect new DB connection from $connection_info.

When $connection_info is not specified, it becomes use already setup connection or it doesn't do at all.

example:

    my $db = Your::Model->new;

or

    # connect new database connection.
    my $db = Your::Model->new(+{
        dsn      => $dsn,
        username => $username,
        password => $password,
        connect_options => $connect_options,
    });

insert

Arguments: $table_name, \%row_data Return: DBIx::Skinny::Row's instance object.

insert new record and get inserted row object.

example:

    my $row = Your::Model->insert('user',{
        id   => 1,
        name => 'nekokak',
    });

or

    my $db = Your::Model->new;
    my $row = $db->insert('user',{
        id   => 1,
        name => 'nekokak',
    });

create

insert method alias.

bulk_insert

Arguments: $table_name, \@row_datas Return: true

Accepts either an arrayref of hashrefs. each hashref should be a structure suitable forsubmitting to a Your::Model->insert(...) method.

insert many record by bulk.

example:

    Your::Model->bulk_insert('user',[
        {
            id   => 1,
            name => 'nekokak',
        },
        {
            id   => 2,
            name => 'yappo',
        },
        {
            id   => 3,
            name => 'walf443',
        },
    ]);

update

Arguments: $table_name, \%update_row_data, \%update_condition Return: updated row count

$update_condition is optional argment.

update record.

example:

    my $update_row_count = Your::Model->update('user',{
        name => 'nomaneko',
    },{ id => 1 });

or

    # see) DBIx::Skinny::Row's POD
    my $row = Your::Model->single('user',{id => 1});
    $row->update({name => 'nomaneko'});

update_by_sql

Arguments: $sql, \@bind_values Return: updated row count

update record by specific sql.

example: my $update_row_count = Your::Model->update_by_sql( q{UPDATE user SET name = ?}, ['nomaneko'] );

delete

Arguments: $table, \%delete_where_condition Return: updated row count

delete record.

example:

    my $delete_row_count = Your::Model->delete('user',{
        id => 1,
    });

or

    # see) DBIx::Skinny::Row's POD
    my $row = Your::Model->single('user', {id => 1});
    $row->delete

delete_by_sql

Arguments: $sql, \@bind_values Return: updated row count

delete record by specific sql.

example:

    my $delete_row_count = Your::Model->delete_by_sql(
        q{DELETE FROM user WHERE id = ?},
        [1]
    });

find_or_create

Arguments: $table, \%values_and_search_condition Return: DBIx::Skinny::Row's instance object.

create record if not exsists record.

example:

    my $row = Your::Model->find_or_create('usr',{
        id   => 1,
        name => 'nekokak',
    });

find_or_insert

find_or_create method alias.

Arguments: $table, \%search_condition, \%search_attr Return: DBIx::Skinny::Iterator's instance object.

simple search method. search method get DBIx::Skinny::Iterator's instance object.

see DBIx::Skinny::Iterator

get iterator:

    my $itr = Your::Model->search('user',{id => 1},{order_by => 'id'});

get rows:

    my @rows = Your::Model->search('user',{id => 1},{order_by => 'id'});

Please refer to DBIx::Skinny::Manual for the details of search method.

single

Arguments: $table, \%search_condition Return: DBIx::Skinny::Row's instance object.

get one record. give back one case of the beginning when it is acquired plural records by single method.

    my $row = Your::Model->single('user',{id =>1});

resultset

Arguments: \%options Return: DBIx::Skinny::SQL's instance object.

result set case:

    my $rs = Your::Model->resultset(
        {
            select => [qw/id name/],
            from   => [qw/user/],
        }
    );
    $rs->add_where('name' => {op => 'like', value => "%neko%"});
    $rs->limit(10);
    $rs->offset(10);
    $rs->order({ column => 'id', desc => 'DESC' });
    my $itr = $rs->retrieve;

Please refer to DBIx::Skinny::Manual for the details of resultset method.

count

get simple count

    my $cnt = Your::Model->count('user', 'id');

search_named

execute named query

    my $itr = Your::Model->search_named(q{SELECT * FROM user WHERE id = :id}, {id => 1});

If you give ArrayRef to value, that is expanded to "(?,?,?,?)" in SQL. It's useful in case use IN statement.

    my $itr = Your::Model->search_named(q{SELECT * FROM user WHERE id IN :ids}, {id => [1, 2, 3]});

search_by_sql

execute your SQL

    my $itr = Your::Model->search_by_sql(q{
        SELECT
            id, name
        FROM
            user
        WHERE
            id = ?
    },[ 1 ]);

txn_scope

get transaction scope object.

    do {
        my $txn = Your::Model->txn_scope;
        # some process
        $txn->commit;
    }

data2itr

    my $itr = Your::Model->data2itr('user',[
        {
            id   => 1,
            name => 'nekokak',
        },
        {
            id   => 2,
            name => 'yappo',
        },
        {
            id   => 3,
            name => 'walf43',
        },
    ]);

    my $row = $itr->first;
    $row->insert; # inser data.

find_or_new

    my $row = Your::Model->find_or_new('user',{name => 'nekokak'});

do

execute your query.

dbh

get database handle.

connect

connect database handle.

reconnect

re connect database handle.

BUGS AND LIMITATIONS

No bugs have been reported.

AUTHOR

Atsushi Kobayashi <nekokak __at__ gmail.com>

CONTRIBUTORS

walf443 : Keiji Yoshimi

TBONE : Terrence Brannon

nekoya : Ryo Miyake

oinume: Kazuhiro Oinuma

fujiwara: Shunichiro Fujiwara

pjam: Tomoyuki Misonou

magicalhat

Makamaka Hannyaharamitu

nihen: Masahiro Chiba

SUPPORT

  irc: #dbix-skinny@irc.perl.org

  ML: http://groups.google.com/group/dbix-skinny

REPOSITORY

  git clone git://github.com/nekokak/p5-dbix-skinny.git  

LICENCE AND COPYRIGHT

Copyright (c) 2010, Atsushi Kobayashi <nekokak __at__ gmail.com>. All rights reserved.

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