DBIx::Foo - Simple Database Wrapper and Helper Functions. Easy DB integration without the need for an ORM.
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
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; }
To install DBIx::Foo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Foo
CPAN shell
perl -MCPAN -e shell install DBIx::Foo
For more information on module installation, please visit the detailed CPAN module installation guide.