Code
I store the ids in a cache to make as little requests to the database as possible. If the group is missing in words_groups table, it will be added.
our %cache;
sub get_group_id
# {{{
{
my $name = shift;
our %cache;
our $dbh;
unless(defined($cache{$name}))
{
my $sql = qq{
SELECT
words_groups.id
FROM
words_groups
WHERE
words_groups.name=?
};
my ($id) = $dbh->selectrow_array($sql, undef, $name);
if (defined($id)) {
$cache{$name} = $id;
} else {
$sql = qq{
INSERT INTO
words_groups
SET
words_groups.name=?
};
$dbh->do($sql, undef, $name);
$cache{$name} = $dbh->{'mysql_insertid'};
}
}
return $cache{$name};
}
# }}}