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.
METHODS
DBIx::Skinny provides a number of methods to all your classes,
- $skinny->new([\%connection_info])
-
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, });
- $skinny->insert($table_name, \%row_data)
-
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', });
- $skinny->create($table_name, \%row_data)
-
insert method alias.
- $skinny->bulk_insert($table_name, \@rows_data)
-
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', }, ]);
- $skinny->update($table_name, \%update_row_data, [\%update_condition])
-
$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'});
- $skinny->update_by_sql($sql, [\@bind_values])
-
update record by specific sql. return update row count.
example:
my $update_row_count = Your::Model->update_by_sql( q{UPDATE user SET name = ?}, ['nomaneko'] );
- $skinny->delete($table, \%delete_condition)
-
delete record. return delete row count.
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
- $skinny->delete_by_sql($sql, \@bind_values)
-
delete record by specific sql. return delete row count.
example:
my $delete_row_count = Your::Model->delete_by_sql( q{DELETE FROM user WHERE id = ?}, [1] });
- $skinny->find_or_create($table, \%values)
-
create record if not exsists record.
return DBIx::Skinny::Row's instance object.
example:
my $row = Your::Model->find_or_create('usr',{ id => 1, name => 'nekokak', });
- $skinny->find_or_insert($table, \%values)
-
find_or_create method alias.
- $skinny->search($table_name, [\%search_condition, [\%search_attr]])
-
simple search method. search method get DBIx::Skinny::Iterator's instance object.
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'});
See "ATTRIBUTES" for more information for \%search_attr.
- $skinny->search_rs($table_name, [\%search_condition, [\%search_attr]])
-
simple search method. search_rs method always get DBIx::Skinny::Iterator's instance object.
This method does the same exact thing as search() except it will always return a iterator, even in list context.
- $skinny->single($table_name, \%search_condition)
-
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});
- $skinny->resultset(\%options)
-
resultset 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;
- $skinny->count($table_name, $target_column, [\%search_condition])
-
get simple count
my $cnt = Your::Model->count('user' => 'id', {age => 30});
- $skinny->search_named($sql, [\%bind_values, [\@sql_parts, [$table_name]]])
-
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.
# SELECT * FROM user WHERE id IN (?,?,?); # bind [1,2,3] my $itr = Your::Model->search_named(q{SELECT * FROM user WHERE id IN :ids}, {id => [1, 2, 3]});
If you give \@sql_parts,
# SELECT * FROM user WHERE id IN (?,?,?) AND unsubscribed_at IS NOT NULL; # bind [1,2,3] my $itr = Your::Model->search_named(q{SELECT * FROM user WHERE id IN :ids %s}, {id => [1, 2, 3]}, ['AND unsubscribed_at IS NOT NULL']);
If you give table_name. It is assumed the hint that makes DBIx::Skinny::Row's Object.
- $skinny->search_by_sql($sql, [\@bind_vlues, [$table_name]])
-
execute your SQL
my $itr = Your::Model->search_by_sql(q{ SELECT id, name FROM user WHERE id = ? },[ 1 ]);
If $opt_table_info is specified, it set table infomation to result iterator. So, you can use table row class to search_by_sql result.
- $skinny->txn_scope
-
get transaction scope object.
do { my $txn = Your::Model->txn_scope; # some process $txn->commit; }
- $skinny->data2itr($table_name, \@rows_data)
-
DBIx::Skinny::Iterator is made based on \@rows_data.
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.
- $skinny->find_or_new($table_name, \%row_data)
-
Find an existing record from database.
If none exists, instantiate a new row object and return it.
The object will not be saved into your storage until you call "insert" in DBIx::Skinny::Row on it.
my $row = Your::Model->find_or_new('user',{name => 'nekokak'});
- $skinny->do($sql, [$option, $bind_values])
-
execute your query.
- $skinny->dbh
-
get database handle.
- $skinny->connect([\%connection_info])
-
connect database handle.
If you give \%connection_info, create new database connection.
- $skinny->reconnect(\%connection_info)
-
re connect database handle.
If you give \%connection_info, create new database connection.
ATTRIBUTES
- order_by
-
{ order_by => [ { id => 'desc' } ] } # or { order_by => { id => 'desc' } } # or { order_by => 'name' }
- for_update
-
{ for_update => 1 }
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.
6 POD Errors
The following errors were encountered while parsing the POD:
- Around line 861:
Unknown directive: =over4
- Around line 863:
'=item' outside of any '=over'
- Around line 1165:
Unknown directive: =over4
- Around line 1167:
You forgot a '=back' before '=head1'
- Around line 1169:
'=item' outside of any '=over'
- Around line 1181:
You forgot a '=back' before '=head1'