NetSDS::DBI(3pm)

πŸ“¦
version 1.301 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute

πŸ’– NetSDS::DBI πŸ’–

okay bestie, gather round, because this is the NetSDS::DBI page from the User Contributed Perl Documentation, and it's literally a whole wrapper around DBI for NetSDS. yes, that NetSDS. she's giving database-handling main character energy and we're obsessed. πŸŽ€


✨ NAME ✨

NetSDS::DBI β€” a DBI wrapper for NetSDS, no cap πŸ’…


🌸 SYNOPSIS 🌸

here's the vibe, copy it, love it, run it:

use NetSDS::DBI;

$dbh = NetSDS::DBI->new(
    dsn    => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432',
    login  => 'user',
    passwd => 'topsecret',
);

print $db->call("select md5(?)", 'zuka')->fetchrow_hashref->{md5};

she's cute, she's compact, she does the thing πŸ¦‹


🫢 DESCRIPTION 🫢

so NetSDS::DBI is a module that provides a wrapper around the DBI module. that's it, that's the whole pitch. iconic behavior honestly. ✨


πŸ‘‘ CLASS API πŸ‘‘

new(%params) β€” the class constructor πŸ’«

bestie she builds you a database handle:

$dbh = NetSDS::DBI->new(
        dsn    => 'dbi:Pg:dbname=test;host=127.0.0.1;port=5432',
        login  => 'user',
        passwd => 'topsecret',
    );

dbh() β€” DBI connection handler accessor πŸ”‘

Returns: a DBI object, periodt.

this method gives you an accessor to the DBI object, so you can get that low level access to database specific methods. iykyk.

Example (access to specific method):

my $quoted = $db->dbh->quote_identifier(undef, 'auth', 'services');
# $quoted contains "auth"."services" now

look at her, quoting identifiers like a queen πŸ‘‘

call($sql, @bind_params) β€” prepare and execute SQL query πŸ”₯

the call() method implements the following functionality, in this exact order:

  • check connection to DBMS and restore it
  • prepare chached SQL statement
  • execute statement with bind parameters

Parameters:

  • SQL query with placeholders
  • bind parameters

Return:

  • statement handler from DBI

Example:

$sth = $dbh->call("select * from users");
while (my $row = $sth->fetchrow_hashref()) {
    print $row->{username};
}

fetch_call($sql, @params) β€” call and fetch result πŸ“

Paramters: SQL query, parameters

Returns: arrayref of records as hashrefs

Example:

# SQL DDL script:
# create table users (
#     id serial,
#     login varchar(32),
#     passwd varchar(32)
# );

# Now we fetch all data to perl structure
my $table_data = $db->fetch_call("select * from users");

# Process this data
foreach my $user (@{$table_data}) {
    print "User ID: " . $user->{id};
    print "Login: " . $user->{login};
}

she hands you a whole perl data structure and you didn't even have to ask nicely πŸ’…

begin() β€” start transaction 🎬

commit() β€” commit transaction βœ…

rollback() β€” rollback transaction πŸ™ˆ

quote() β€” quote SQL string 🧚

Example:

# Encode $str to use in queries
my $str = "some crazy' string; with (dangerous characters";
$str = $db->quote($str);
ngl, that string is full of dangerous characters, so let her do the quoting, fr fr. don't rawdog your SQL. πŸ’€

🌈 INTERNAL METHODS 🌈

these are the behind-the-scenes girlies, not for your everyday group chat.

_add_sets() β€” add initial SQL query ⭐

Example:

$obj->_add_sets("set search_path to myscheme");
$obj->_add_sets("set client_encoding to 'UTF-8'");

_add_attrs() β€” add DBI handler attributes πŸ’œ

$self->_add_attrs(AutoCommit => 1);

_check_connection() β€” ping and reconnect πŸ”„

Internal method checking connection and implement reconnect.

_connect() β€” connect to DBMS 🌐

Internal method starting connection to DBMS.


πŸŽ€ EXAMPLES πŸŽ€

  • samples/testdb.pl

go peek, she's right there 🫢


πŸ‘€ SEE ALSO πŸ‘€

  • DBI
  • DBD::Pg

πŸ’€ TODO πŸ’€

  1. Make module less PostgreSQL specific.

(it's giving very-specific, and we respect the self-awareness 🫢)


🌟 AUTHOR 🌟

Michael Bochkaryov <misha@rattler.kiev.ua>


πŸ“œ 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


okay that's the whole page bestie, mwah πŸ’‹ this one's from perl v5.32.1, dated 2021-12-26 β€” stay slaying, and remember: always bind your params, never trust a rogue apostrophe, and log off occasionally πŸŒΈπŸ’–βœ¨