Nothing prevents you from using the full file name in the "pagename" column :)

Regarding how to get the page name, you provided little insight about how a page name is "generated"; in any case, whatever your approach (the one suggested by Fletch, or the ALTER TABLE one), you have to find some way to decide which counter you want to increase. Which means that this is not a problem tied to this specific solution. If the page name is tied to the current script, anyway, you might benefit from the contents of either $0 (perlvar) or of __FILE__ (perldata), whatever applies best to your case.

I'd make the "pagename" column a primary key for the table, and just do something like this:

# Unconditionally try to create a new record. eval { $db->do('INSERT INTO counters (pagename, pagecounter) VALUES ($, 0) +', undef, $pagename); }; # Unconditionally increase the counter $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename);
If the record for the given page does not exist, it's created and the counter is initialised to 0. If it already exists, the INSERT fails due to the fact that the pagename column is a primary key, and you can't have two records with the same primary key. In either case, after the eval you're sufficiently confident* that a record for the given page actually exists in the table.

After this, you increase the counter. If the record was just created, it is correctly bumped to 1. In the other case, it is simply increased, which is what you want.

No checks (the RDBMS does these for you), no race conditions... maybe a little dirt? Acceptable, IMHO.

* "sufficiently confident" means that the INSERT could fail for other reasons apart the field being there. If all the rest is working properly, than you can be confident that after the eval you'll have something to increase in that table.

Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Io ho capito... ma tu che hai detto?

In reply to Re^5: Making an automatic counter adder for all webpages by polettix
in thread Making an automatic counter adder for all webpages by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.