NetSDS...:Table(3pm)
π NAME π
NetSDS::DBI::Table β the Perl module that handles all your boring database CRUD so you don't have to, bestie. It lives in the user-contributed Perl docs, section 3pm. π
β¨ SYNOPSIS β¨
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();
πΈ DESCRIPTION πΈ
Okay so NetSDS::DBI::Table gives you the CRUD functionality you use literally all the time, for data chillin' in a single database. No cap.
The whole main character idea here is that we can all agree on a few limitations up front:
- every table like this has an
idfield, and that's the primary key π - we're using PostgreSQL, and we're using it with all of its features, periodt π
π CLASS API π
π¦ new([...]) β class constructor π¦
Builds you a fresh object. Iconic behaviour, honestly.
my $tbl = NetSDS::DBI::Table->new(
dsn => 'dbi:Pg:dbname=content',
login => 'netsds',
passwd => 'topsecret,
table => 'content.meta',
);
π fetch(%params) β get records from table as array of hashrefs π
Parameters (a hash, obvi):
fieldsβ fetch fields by listfilterβ arrayref of SQL expressions like"status = 'active'"for theWHEREclauseorderβ arrayref of SQL expressions like"id desc"for theORDER BYclauselimitβ 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, bestie:
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
)
π§ insert_row(%key_val_pairs) β insert record into table π§
Parameters: record fields as hash
Returns: id of inserted record
my $user_id = $tbl->insert_row(
'login' => 'vasya',
'password' => $encrypted_passwd,
);
π insert(@records_list) β mass insert π
Parameters: list of records (as hashrefs)
Returns: array of inserted records id
This method lets you mass insert records, and it ate:
my @user_ids = $tbl->insert(
{ login => 'vasya', password => $str1 },
{ login => 'masha', password => $str2 },
{ login => 'petya', password => $str3, active => 'false' },
);
β οΈ Warning! This method uses separateINSERTqueries and is literally just a wrapper around multipleinsert_row()calls. So it's not as fast as one big insert β but the trade-off is it lets you use different key-value pairs for different records. Slay, but know what you're signing up for. π
π update_row($id, %params) β update record parameters π
Parameters: 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, the %upd hash will contain the updated table record. π«Ά
π₯ update(%params) β update records by filter π₯
Parameters: filter, new values
$tbl->update(
filter => ['active = true', 'created > '2008-01-01'],
set => {
info => 'Created after 2007 year',
}
);
β get_count(%params) β retrieve number of records β
Just returns the total number of records, by calling:
# SELECT COUNT(id) FROM schema.table
my $count = $tbl->get_count();
my $count_active = $tbl->get_count(filter => ['active = true']);
π delete_by_id(@ids) β delete records by identifier π
Parameters: list of record id
Returns: 1 if ok, undef if error
This method deletes records from the SQL table by its identifiers.
if ($tbl->remove(5, 8 ,19)) {
print "Records successfully removed.";
}
π
delete(@filters) β delete records π
Parameters: list of filters
Returns: 1 if ok, undef if error
$tbl->delete(
'active = false',
'expire < now()',
);
πΈ get_fields() β get list of fields πΈ
Example:
my @fields = @{ $tbl->get_fields() };
print "Table fields: " . join (', ', @fields);
π¦ has_field($field) β check if field exists π¦
Parameters: field name
Example:
if ($tbl->has_field('uuid')) {
$tbl->call("delete tbldata where uuid=?", $uuid);
}
NOTE: this method works only for restricted tables that use the fields parameter at construction time. So like, don't come for it if you didn't set that up, bestie. π
π EXAMPLES π
Go look at the samples/test_db_table.pl script. It's right there. β¨
π BUGS π
Bad documentation. (Their words, not mine β but honestly? Respect the honesty. π)
π SEE ALSO π
π TODO π
None. Periodt. π
π AUTHOR π
Michael Bochkaryov <misha@rattler.kiev.ua> β the main character who wrote this. π«Ά
π LICENSE π
Copyright (C) 2008-2009 Net Style Ltd.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
and that's the whole page, bestie π this one's from perl v5.32.1, dated 2021-12-26, and it's giving "old but iconic". go build something cute with it π§β¨
π¦ where this page comes from
- debian bookworm: libnetsds-perl 1.301-4 Β· GPL-2.0-or-later
- debian trixie: libnetsds-perl 1.301-4 Β· GPL-2.0-or-later
- ubuntu jammy: libnetsds-perl 1.301-4 Β· GPL-2.0-or-later
- ubuntu noble: libnetsds-perl 1.301-4 Β· GPL-2.0-or-later
- ubuntu resolute: libnetsds-perl 1.301-4 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms