Cracking a database / Recursion
March 26, 2006 · 4 Comments
Or what I did at the weekend...
A cat breeder friend was recently bemoaning the fact that their old, old pedigree program no longer runs on their new computer so they were locked out of their data about almost 2,000 cats and their pedigrees. I said to send me the data file and I'd try to crack it open for them.
What turned up was a ZIP file containing, amongst other things, a file roughly a quarter of a megabyte in size that seemed to be the database itself. Running the strings command confirmed that this file did indeed contain all of the cat names, colors and registration numbers. Running od -c produced an "octal dump" with character data and by observation I figured out that the useful data started 512 bytes into the file and each record was a fixed 128 bytes long.
I asked my wife for a couple of reference cat names and their sire/dam so I could figure out what fields in the rest of the data represented those foreign keys. Then it was time to pull the file apart.
Much as I love ColdFusion, it's not the right tool for this job. Instead, I fired up vi and rattled off a bunch of procedural C code to rip the data out. A quick union of unsigned char[128] and a struct containing the fields I knew about provided the input buffer and a dirty little printf helped me output CSV to the console.
Now I had CSV data and ColdFusion is great with that so I knocked up a quick table create / load script and populated MySQL with the data.
From that point on, it was easy to create a little application that could search cat data and generate five-generation pedigrees. And that's where recursion comes in...
If I want a five-generation pedigree of a cat, what I need is a four-generation pedigree of each of its parents. And a three-generation pedigree of each of those cats' parents. And so on.
To create a very simple pedigree using nested tables, a quick and dirty recursive UDF can walk through the database, building HTML for each cat and calling itself to get the HTML for that cat's parents.
You can play with the pedigree application if you want. The code? Well, it's a raw, single page proof of concept, just over 100 lines of code. But it deliberately uses event= in the URL in preparation for converting it to a Model-Glue application. A Model-Glue, ColdSpring, Reactor application, to be precise. I may release the before and after code in due course...
Tags: coldfusion

4 responses so far ↓
1 Roland Collins // Mar 26, 2006 at 5:14 PM
2 Sean Corfield // Mar 26, 2006 at 11:01 PM
Oracle also has recursive relationships so I'm used to the feature.
Most all of what I work on these days is MySQL (MySQL 4 on my ISP, unfortunately, rather than MySQL 5).
3 jason // Mar 27, 2006 at 6:50 AM
ie i would love to see the code for this!
4 John Allen // Apr 5, 2006 at 4:39 PM
I would LOVE to see the code as well.
Old databases, cats, friends. Like i said, you are cool.
Leave a Comment