RoPkg::DBObject(3pm)

πŸ“¦ version 0.4, in ubuntu jammy

✨ RoPkg::DBObject(3pm) ✨

okay bestie, this is the user-contributed Perl documentation for RoPkg::DBObject, and honestly? it's giving "the database object to end all database objects" πŸ’… it's a manual page for a Perl module, it lives in section 3pm, and it's here to make your objects and your database hold hands. let's get into it 🫢


πŸ’– NAME πŸ’–

RoPkg::DBObject β€” a general-purpose database object. that's it, that's the whole vibe: one object, one database, endless slay ✨


πŸŽ€ VERSION πŸŽ€

0.1.4</purl> β€” wait no, 0.1.4 🌸 (tiny but mighty, fr fr)


πŸ¦‹ DESCRIPTION πŸ¦‹

so the way that this works: this class can be used as a base class for objects who keep all their info in a database. she provides the base methods to add/delete/update/select the object to/from a database. iconic behaviour honestly πŸ”₯

and yes, RoPkg::DBObject inherits RoPkg::Object. family tree matters πŸ‘‘


🌈 SYNOPSIS 🌈

here's the whole thing, verbatim, no notes, because code is sacred 🧚

package RoPkg::Person;

use strict;
use warnings;

use RoPkg::DBObject;
use RoPkg::Exceptions;

use vars qw($VERSION @ISA);

@ISA=qw(RoPkg::DBObject);

my $methods = {
  id => '-',
  Name => '-',
  Addr => '-',
};

sub new {
  my ($class, %opt) = @_;
  my $self;

  $opt{methods} = $methods;
  $self = $class->SUPER::new(%opt);

  return $self;
}

sub AddToDB {
  my ($self) = @_;

  OutsideClass->throw('Called from outside class')
    if ( !$self or ref($self) ne $CLASS);

  return $self->SQL_Insert();
}

1;

tester.pl

use warnings;
use strict;

use RoPkg::Person;
use RoPkg::DB;

sub main {
  my ($p, $db);

  $db = new RoPkg::DB();
  $db->add('dsn...', 'user', 'pass', 'dbc');
  $p = new RoPkg::Person(dbo => $db, dbo_method => 'db_dbc');

  $p->id(1);
  $p->Name('John Doe');
  $p->Addr('home');
  $p->AddToDB();
}

main();

like, look at her. setting @ISA, building $methods, throwing OutsideClass when somebody tries to be slick and call it from outside the class. main character energy the entire time πŸ’…


πŸ’œ SUBROUTINES/METHODS πŸ’œ

🌸 new($hashref) 🌸

the constructor of the class, the birth of the object, the origin story ✨ at this moment new() accepts 3 parameters:

  • dbo
  • dbo_method
  • quote_char

so dbo and dbo_method must be given to the constructor. quote_char is optional, she's chill.

  • dbo is the database object (a instance of RoPkg::DB) πŸ’Ύ
  • dbo_method is the name of the method used to have access to the database handle πŸ”‘
  • quote_char is the char used internally by DBI to quote the table names, field names and so on. default: the quote_char is empty (no quotation, live your truth). but bestie listen β€” if you are using PostgreSQL and you have upcase chars in table and fields names, use quote_char => q{"} πŸŽ€

🫢 Exceptions 🫢

RoPkg::DBObject uses the exceptions provided by RoPkg::Exceptions. new() throws the following exceptions:

  • Param::Mising (sic ✨ that's literally how it's spelled in the list, no I will not fix it, the vibes are immaculate)
  • Param::Wrong

Param::Missing is raised when dbo or dbo_method parameters have not been specified in the parameters list πŸ™ˆ

Param::Wrong is raised when dbo or dbo_method parameters are not defined (or empty) πŸ’€

πŸ¦‹ dbo($dbo) πŸ¦‹

a get/set method, the classic dual-threat πŸ‘‘

if $dbo is present (and defined), the internal $dbo object will be replaced with the new one. if $dbo is not present, the method acts as a get method, and will return the database object.

⚠️ the method will raise OutsideClass exception if is called outside the class instance. don't try it. she'll know. πŸ’…

πŸŽ€ dbo_method($dbo_method) πŸŽ€

another get/set method, she's consistent πŸ’–

if $dbo_method is present (and defined), the internal $dbo_method value will be replaced with the one specified by the $dbo_method parameter. otherwise, the get behaviour will be selected, and the method will return the current value of $dbo_method.

⚠️ same deal: the method will raise OutsideClass exception if is called outside the class instance. boundaries, bestie 🫢

⭐ dbh() ⭐

returns the DBI object used by this object. that's it, that's the whole method, short and slay 🌸


πŸ”₯ SQL METHODS πŸ”₯

these following methods are responsable for all operations involving the database. every single one of them uses SQL::Abstract to generate sql queries, so that the queries stay portable. no cap, that's the whole point πŸ”₯

and the data that will be added/deleted to the database is taken from the methods provided by $methods. iykyk 🧚

πŸ’Ύ SQL_Insert() πŸ’Ύ

add the current object to the database. returns the value of DBI->execute. 🫢

🌟 SQL_Update(@fields) 🌟

update the data from database. the fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object) πŸ‘‘

πŸ“ SQL_Delete(@fields) πŸ“

update the data from database. the fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object) πŸ™ˆ (yes, the manpage says "update" here too β€” I'm just telling you what she said, don't shoot the messenger πŸ’€)

πŸ¦‹ SQL_Select(@fields) πŸ¦‹

searches into the database for the object and initialize the current object with the values found. the fields array hold all the field names who will uniquely identify the object in the database (usually the id of the object).

this method is special ✨ she will look only at the first record who meet the requirements (in sql 'LIMIT 1'). also, if no records are found, DB::NoResults exception is raised. so like, expect drama if the row isn't there 😭


🧚 DEPENDENCIES 🧚

RoPkg::Object require perl 5.008 or later and the following modules:

  • SQL::Abstract
  • RoPkg::Object
  • RoPkg::Exceptions

πŸ’… PERL CRITIC πŸ’…

the code is perl critic level 2 compliant and almost level 1 compliant. almost!! so close!! she's trying her best and honestly we stan a self-aware queen πŸ‘‘


πŸŽ€ DIAGNOSTICS πŸŽ€

this module comes with his own tests πŸ§ͺ to run the tests, unpack the source, and use the 'make test' command. go off, verify her 🌸


🌈 CONFIGURATION AND ENVIRONMENT 🌈

this module does not use any configuration files or environment variables. the dependencies however can use them β€” so please read the man page of each dependency to be sure. do your homework bestie 🫢


πŸ¦‹ INCOMPATIBILITIES πŸ¦‹

⚠️ Do not install this version on the same machine with Simba <= 0.7.1 ⚠️

like, literally do not. they cannot coexist. it's giving exes at the same party πŸ’€


πŸ™ˆ BUGS AND LIMITATIONS πŸ™ˆ

none known to the author. iconic. flawless. no notes. (for now πŸ‘€)


πŸ’– AUTHOR πŸ’–

Subredu Manuel <diablo@iasi.roedu.net> β€” the legend behind the whole thing, thank you for your service 🫑


Copyright (C) 2005 Subredu Manuel. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. the LICENSE file contains the full text of the license πŸ“œ


okay that's the whole page bestie, you're officially educated πŸ’… this was perl v5.32.1, dated 2021-12-26, and it's the manual page for RoPkg::DBObject(3pm) β€” go build something iconic with it πŸŒΈβœ¨πŸ¦‹

πŸ“¦ where this page comes from