By Mike Hanson

As developers of database applications, auto-numbering is something that we need to accomplish with virtually every table that we create.  (At least we should be doing that.)  There are two main purposes for an auto-numbered field:

How Does Clarion Do It?

In the very early days of Clarion, auto-numbering had to be done manually.  It wasn’t too long, though, before Clarion gave us a very handy method to achieve auto-numbering directly in the options for the key.  As long as the key was marked as unique, it could also be auto-numbered.  Not only that, it automatically handled multi-component keys, so you could auto-number detail rows associated an invoice header.

The main weakness with this approach is that it creates a placeholder record to reserve the new ID when you first express your intention to add that new item (e.g. when you first enter a form with Request=Insert).  Assuming you press OK when you’re done, that placeholder record is updated with the final values for the rest of the fields.  If you cancel from the form, then the placeholder is deleted, risking a chance that another user added a record after it, so that your cancelled number is never be used.  (We all know how much auditors love gaps in the numbering.)

Another downside is that initially blank values in the rest of the record’s fields could cause an error, if the blank fields are included in other unique keys.  Working around this involves priming those fields with some value that prevents the duplicate key error.

Yet another problem is that those placeholder records are sitting in the database with mostly blank fields, which could be seen by other users in browses, reports and processes.  Tweaking all of those procedures to prevent the appearance of these placeholders is an arduous task.  And if the user’s system dies mid-insert, these remnants are left mucking up the database.

So why would Clarion do it this way, rather than determining the number when the record insert is complete?  The main reason is that Clarion users expect it to work that way:

To move away from using the placeholder record, we must decide how to handle the two points above.

Auto-Numbering Alternative: UUID

It’s worth mentioning that for primary keys, you don’t strictly need an auto-numbered field.  It must merely be unique.  You can use a “UUID” (Universally Unique ID), also known as a GUID (Globally Unique ID).  The terms are often interchanged, but strictly speaking a GUID is considered to be a Microsoft variant of a UUID.  You could use a Microsoft GUID for your primary key field, or simply use a STRING field with random characters and numbers (i.e. Base 36).  Ensure that the field is long enough for the value to be unique (e.g. 24 characters is roughly equivalent to a standard GUID), and assign the random value when the field is primed.

Pros:

Cons:

Do I Really Have to Change?

The short answer is “No.”

You may be thinking that this is sounding like a lot of trouble for little benefit.  If you don’t mind all of the issues with client-side auto-numbering using placeholder records (which you’ve always lived with in your ISAM system), then you can continue to let Clarion do things as it did before (even with SQL).  It will still work, with all of the aforementioned caveats.

However, now that you’ve decided to move to SQL, you may as well try to do things the right way.  SQL provides many opportunities to increase performance, improve stability, work with non-Clarion software, etc.  There are certain expectations that should be met when you do that.

In the end, though, it’s your decision.  If you’re still interested in learning more, then please read-on.

On with the Show!

Now that you understand the pros and cons of Clarion’s auto-numbering, let’s look at how SQL fits into the picture.  Unfortunately, it’s not as easy as flicking a switch in the key definition in your dictionary.  Clarion’s database drivers do a great job of making most database access generic, but auto-numbering is done differently with each engine (and sometimes different versions of SQL flavors change how they do it with new versions).  That’s why you must approach auto-numbering as a “custom” task.

In this first article I’ll list the key areas that you must address, followed by a series of articles with the specific steps using a variety of database engines (PostgreSQL, Microsoft SQL Server, SQLite…).  You can read this initial overview, then jump to the corresponding article for your engine of choice.  I doubt that I’ll cover all possible back ends, but hopefully I'll visit enough to help the majority of Clarion users.

I will also touch upon various points when using tools like DMC and File Manager 3, both of which I use.

Here are the main areas that will require our attention:

I glossed over many of the particulars in the points above, as it makes more sense to include the precise details as I describe the steps for each database engine.  First up will be PostgreSQL.

I’ve found that auto-numbering is a far more complex topic that I initially expected.  I’m trying to distill all of those complexities, options and decisions into a simple set of rules.  If you feel that I missed anything crucial or misspoke, then please feel free to provide feedback.  I’ll occasionally update this article as my understanding and viewpoint evolves.