parse_header

Input

  1. $name — group name for words_groups tables

Internal data

words_groups
id name
1 group 1
2 group 2

Output

Id of the record in words_groups table, corresponding $name.

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};
}

# }}}