use DBI::Sugar;
DBI::Sugar::factory {
return DBI->connect(...);
};
# open a new transaction
TX {
# select some rows
my %rows = SELECT "id, name, surname
FROM people WHERE age >= ? AND age < ?"
=> [18,25]
=> sub {
$_{id} => join ' ', $_{name}, $_{surname};
};
# %rows => (
# 123 => "Adam Smith",
# 345 => "John Brown",
# ...
# );
# quite straightforward
SQL_DO "DELETE FROM myTab ORDER BY id ASC LIMIT ?" => [1];
# tired of autoincrements or sequences?
my $next;
TX_NEW { # in a new transaction
# fetch the next id
(undef, $next) = SELECT_ROW "next FROM ids WHERE type = ?
FOR UPDATE" => [$type];
# update the table
SQL_DO "UPDATE ids SET next = next + 1 WHERE type = ?" => [$type];
}; # commit
# insert
INSERT myTab => {
id => $next,
a => "Foo",
b => "Bar",
};
# update
UPDATE myTab => {
id => $next,
} => {
ct => ['ct + ?', $inc],
}
my $id = NEXT_ID myTab => 10;
# reserve 10 ids from table "ids" where name = 'myTab'
# returns the first, keep the other 9 in a pool
UPSERT myTab => {
# WHERE
key => $id,
} => {
# UPDATE
ct => ['ct + ?', $inc],
last_mod => ['NOW()'],
} => {
# INSERT
ct => $inc, # override ct
# last_mod is ok as in the update
created => ['NOW()'], # but created need to be set
}
};
# commit if it returns, rollback if it dies
To install this module, run the following commands:
perl Build.PL
./Build
./Build test
./Build install
After installing, you can find documentation for this module with the perldoc command.
perldoc DBI::Sugar
You can also look for information at:
GitHub
http://github.com/ohait/perl-dbi-sugar
Copyright (C) 2014 Francesco Rivetti
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.