NetSDS::DBI::Table
use NetSDS::DBI::Table; my $q = NetSDS::DBI::Table->new( dsn => 'dbi:Pg:dbname=netsdsdb;host=127.0.0.1', user => 'netsds', passwd => 'test', table => 'public.messages', ) or warn NetSDS::DBI::Table->errstr();
NetSDS::DBI::Table module provides commonly used CRUD functionality for data stored in single database.
Main idea was that we can agree about some limitations:
* every such table contains id field that is primary key
id
* we use PostgreSQL DBMS with all it's features
my $tbl = NetSDS::DBI::Table->new( dsn => 'dbi:Pg:dbname=content', login => 'netsds', passwd => 'topsecret, table => 'content.meta', );
Paramters (hash):
* fields - fetch fields by list
* filter - arrayref of SQL expressions like status = 'active' for WHERE clause
status = 'active'
WHERE
* order - arrayref of SQL expressions like id desc for ORDER BY clause
id desc
ORDER BY
* limit - max number of records to fetch (LIMIT N)
* offset - records to skip from beginning (OFFSET N)
* for_update - records selected for further update within current transaction
Returns: message as array of hashrefs
Sample:
my @messages = $q->fetch( fields => ['id', 'now() as time'], filter => ['msg_status = 5', 'date_received < now()'], # where msg_status=5 and date_received < now() order => ['id desc', 'src_addr'], # order by id desc, src_addr limit => 3, # fetch 3 records offset => 5, # from 6-th record for_update => 1, # for update )
Paramters: record fields as hash
Returns: id of inserted record
my $user_id = $tbl->insert_row( 'login' => 'vasya', 'password' => $encrypted_passwd, );
Paramters: list of records (as hashrefs)
Returns: array of inserted records "id"
This method allows mass insert of records.
my @user_ids = $tbl->insert( { login => 'vasya', password => $str1 }, { login => 'masha', password => $str2 }, { login => 'petya', password => $str3, active => 'false' }, );
Warning! This method use separate INSERT queries and in fact is only wrapper for multiple insert_row() calls. So it's not so fast as one insert but allows to use different key-value pairs for different records.
insert_row()
Paramters: id, new parameters as hash
Returns: updated record as hash
Example:
my %upd = $table->update_row($msg_id, status => 'failed', dst_addr => '380121234567', );
After this %upd hash will contain updated table record.
Paramters: filter, new values
$tbl->update( filter => ['active = true', 'created > '2008-01-01'], set => { info => 'Created after 2007 year', } );
Just return total number of contacts by calling:
# SELECT COUNT(id) FROM schema.table my $count = $tbl->get_count();
Paramters: list of record id
Returns: 1 if ok, undef if error
Method deletes records from SQL table by it's identifiers.
if ($tbl->remove(5, 8 ,19)) { print "Records successfully removed."; }
Paramters: list of filters
$tbl->delete( 'active = false', 'expire < now()', );
See samples/test_db_table.pl script
samples/test_db_table.pl
Bad documentation
NetSDS::DBI http://en.wikipedia.org/wiki/Create,_read,_update_and_delete
None
Michael Bochkaryov <misha@rattler.kiev.ua>
To install NetSDS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm NetSDS
CPAN shell
perl -MCPAN -e shell install NetSDS
For more information on module installation, please visit the detailed CPAN module installation guide.