Extracting and Sorting Data

You may sometimes need to create a report that cannot efficiently be created using the indexes built into the database. For example you might need to produce a report sorted by some derived value not present on the database.

Assuming you do not decide to add the information and a suitable index to the database, you will be faced with having to sort the data. DP4 makes this easy for you with the concept of extract files.

Both of these are described in Extracting and Sorting Records in the LIBMAKE documentation.

A program that uses extract files usually follows this pattern:

  1. After initialising the program trawls through the database calling the function extract_write() to write unsorted entries to the extract file

  2. When all the necessary data has been read the extract file is sorted by calling extract_sort(). Depending on the size of the file and the degree of order already present this may take just a second or two, or perhaps many minutes or even hours if the file is extremely large. However, so far as we know, the sort algorithm we use would be very hard to improve upon.

  3. Once the sort has completed your program reads the now sorted extract file by calling extract_read() repeatedly, and uses the information to create the required report.

The extract file routines are supplied in source code form (in ccextrct.c), so you can create a customised version of them if required. The source code of the sort program is not distributed.