Catmandu-DBI
view release on metacpan or search on metacpan
lib/Catmandu/Store/DBI.pm view on Meta::CPAN
printf "obj1 stored as %s\n" , $obj1->{_id};
# Force an id in the store
my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' });
my $obj3 = $store->bag->get('test123');
$store->bag->delete('test123');
$store->bag->delete_all;
# All bags are iterators
$store->bag->each(sub { ... });
$store->bag->take(10)->each(sub { ... });
=head1 DESCRIPTION
A Catmandu::Store::DBI is a Perl package that can store data into
DBI backed databases. The database as a whole is a 'store'
L<Catmandu::Store>. Databases tables are 'bags' (L<Catmandu::Bag>).
Databases need to be preconfigured for accepting Catmandu data. When
no specialized Catmandu tables exist in a database then Catmandu will
create them automatically. See "DATABASE CONFIGURATION" below.
DO NOT USE Catmandu::Store::DBI on an existing database! Tables and
data can be deleted and changed.
=head1 CONFIGURATION
=over
=item data_source
Required. The connection parameters to the database. See L<DBI> for more information.
Examples:
dbi:mysql:foobar <= a local mysql database 'foobar'
dbi:Pg:dbname=foobar;host=myserver.org;port=5432 <= a remote PostGres database
dbi:SQLite:mydb.sqlite <= a local SQLLite file based database mydb.sqlite
dbi:Oracle:host=myserver.org;sid=data01 <= a remote Oracle database
Drivers for each database need to be available on your computer. Install then with:
cpanm DBD::mysql
cpanm DBD::Pg
cpanm DBD::SQLite
=item user
Optional. A user name to connect to the database
=item password
Optional. A password for connecting to the database
=item timeout
Optional. Timeout for a inactive database handle. When timeout is reached, Catmandu
checks if the connection is still alive (by use of ping) or it recreates the connection.
See TIMEOUTS below for more information.
=item reconnect_after_timeout
Optional. When a timeout is reached, Catmandu reconnects to the database. By
default set to '0'
=back
=head1 DATABASE CONFIGURATION
When no tables exists for storing data in the database, then Catmandu
will create them. By default tables are created for each L<Catmandu::Bag>
which contain an '_id' and 'data' column.
This behavior can be changed with mapping option:
my $store = Catmandu::Store::DBI->new(
data_source => 'DBI:mysql:database=test',
bags => {
# books table
books => {
mapping => {
# these keys will be directly mapped to columns
# all other keys will be serialized in the data column
title => {type => 'string', required => 1, column => 'book_title'},
isbn => {type => 'string', unique => 1},
authors => {type => 'string', array => 1}
}
}
}
);
For keys that have a corresponding table column configured, the method 'select' of class L<Catmandu::Store::DBI::Bag> provides
a more efficiënt way to query records.
See L<Catmandu::Store::DBI::Bag> for more information.
=head2 Column types
=over
=item string
=item integer
=item binary
=back
=head2 Column options
=over
=item column
Name of the table column if it differs from the key in your data.
=item array
( run in 1.840 second using v1.01-cache-2.11-cpan-e93a5daba3e )