The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Foo - Simple Database Wrapper and Helper Functions. Easy DB integration without the need for an ORM.

SYNOPSIS

DBIx::Foo::SimpleQuery

my $dbh = DBIx::Foo->connect(...) # or ->new

my $rows = $dbh->selectall("select * from test");

my $row = $dbh->selectrow("select * from test where ID = ?", 1); # alias for selectrow_hashref

DBIx::Foo::UpdateQuery

This can be used to build a query for writing to the database. First an insert statement:

my $query = $dbh->update_query('test');

$query->addField(Name => 'Foo');
$query->addField(Desc => 'Bar');

my $newid = $query->DoInsert;

And with very similar syntax, an update statement:

my $query = $dbh->update_query('test');

$query->addKey(ID => $newid);
$query->addField(Name => 'Fu');
$query->addField(Desc => 'Baz');

$query->DoUpdate;

This works nicely with data in a hash, which can be interated and used to update or insert as appropriate, based the existence of a key field:

my $data = {
 	Name => 'Foo',
 	Desc => 'Bar',
};

foreach my $field (key %$data) {

 	$query->addField($field => $data->{$field}) if $data->{$field};
}

if (my $id = $data->{ID}) {

  # updating
 	$query->addKey(ID => $id);

 	$query->DoUpdate;
}
else {

  # inserting
  my $newid = $query->DoInsert;
}