![]() |
| > Home > Documentation > Latest documentation > Interchange tags > counter |
|
counterManipulates a persistent counter, by default incrementing it and returning the new value. Summary
ExamplesPerl example
or similarly with positional parameters:
DescriptionManipulates a persistent counter, by default incrementing it and returning the new value. The counter value is stored in the specified file. If the file name begins with a "/" then it is an absolute path. Otherwise, it is relative to website's home directory, as specified using the Catalog global configuration directive. The default file is etc/counter. If the file does not exist, it is created and initialised to the value of the start parameter. If the optional sql attribute is used, a SQL sequence will be used. Currently MySQL and Postgres are supported. The sequence must already exist. If no bypass parameter is sent, the table in the sequence callout (explained below) will be used and must be an Interchange table (i.e. set up with Database setting). If bypass is set then the DSN for the sequence will be passed in the dsn parameter. If the optional date parameter is used, a date-based counter will be made. Date-based counters take the form of the date in YYYYMMDD, followed by the start value, by default 0001. When the date changes, the counter will flip over to the next day and the beginning start value.
SQL Counter ExamplesTo set up a PostgreSQL counter, simply create a named sequence in the database:
You will want to make sure you have an Interchange table that uses that database ("sometable" in the example, below). Then access it with:
You can create as many sequences as you like. To set up a MySQL counter, add a table to your MySQL database into catalog.cfg or other place like dbconf/mysql:
Make sure you set up the sequence.txt file if you want Interchange to create the table for you. Otherwise you can create the table yourself from a MySQL client:
Then access it with:
Alternatively, you can create the table without Interchange definition, as long as it is in the same database as an Interchange table:
Then access it by calling the Interchange-defined table name followed by the table that has the AUTO_INCREMENT key:
To set up an Oracle counter, create a sequence:
Then access via a table already connected to Oracle, use the following:
ParametersfileAn actual text file that will be used to store the current count. decrementCauses the counter to count down instead of up. This option is not applicable to SQL counters. startCauses a new counter to be created and to start from 50 (for example) if it did not exist before. This option is not applicable to SQL counters. valueShows the value of the counter without incrementing or decrementing it. This option is not applicable to SQL counters. sqlThe table and sequence name of the SQL counter. If the type of database is Postgres, the table is used to derive the database and the sequence will be a named sequence independent of the table; the sequence is incremented and accessed by "SELECT nextval('sequence_name')". If the type of database is MySQL, an AUTO_INCREMENT key column is assumed and an insert of 0 followed by "select last_insert_id()" will increment then access the counter. dateThis parameter can have one of two values, as follows:
Either way, the counter will be formatted as "%Y%m%d" (see the list of time format tokens), followed by a four-digit number. By default, the number starts counting from "0001", but can be overridden with the use of the start parameter, as usual.
|
| Home | Legal nonsense | Privacy policy | Contact us |