This manual gathers together the key insights into API design that were discovered through many years of software development on the Qt application development framework at Trolltech (now part of Nokia). When designing and implementing a library, you should also keep other factors in mind, such as efficiency and ease of implementation, in addition to pure API considerations. And although the focus is on public APIs, there is no harm in applying the principles described here when writing application code or internal library code.
Simon Holywell
Posts tagged code
SQL Server 2005 Dump to SQL statements→
Say you have a development environment setup using SQL Server 2005 Express Edition and your customer has a SQL Server 2000 database accessible only via ODBC and you can only run a DB import via a script. How would you do it? Easy you might think just run msdbdump.exe on the command line, well this isn’t MySQL so you are not so fortunate.
Backing up your DB is easy with SQL Server using the following commands:
But this just gives you a binary file that can be restored if you have access privileges to the live database for restoring. If you are in a shared hosting environment or one where the paranoid admins won’t give you remote desktop access and the only access you have is to run a PHP script to import the data and schema via SQL you will need to export the DB to T-SQL format.
Microsoft have a little program to perform this very function: Microsoft SQL Server Database Publishing Wizard 1.1 It is difficult to find on the web so I aim to save you the time I spent hunting for it. When you run the wizard make sure to set:
- Drop existing objects in script to false
- Schema qualify to false
- Script for target database to SQL Server 2000
It does seem to chew on the cud for quite some time so grab a beverage.
Now for the PHP portion of the process. So you have uploaded your lovely T-SQL dump file to a PHP accessible location on your webserver and now you are wondering how to get into your DB via ODBC. Well you will need a PHP script like the one I have supplied below.
A couple of the complexities to be aware of before you continue. It seems that the T-SQL dump file comes out as UTF16 and we need it in UTF8 so you will need to convert it to UTF8 before you can import. I used a neat little function available from Modular.org for this purpose. This may or may not meet your needs. If you need a more accurate conversion method then I recommend you start your search with the PHP module/function mbstring . T-SQL contains reference and keywords that ODBC/MS SQL cannot understand. I have included some regex to strip these out.
My script is by no means perfect or factored down so feel free to make suggestions or improvements.
ISO 3166 Country List→
Here is a small script that I put together to grab the XML list from the ISO website and convert it into an SQL statement. The idea is that the script can be used to just obtain a list for web projects or it can be setup to run periodically to make sure that a list is always kept up to date. Obviously none of the associated checks to make that work have been written into this code so you will need to add it yourself (ie. checking that if a country has been removed that you are not going to break referential integrity etc).
It is a really simple script so just use it as you wish, but make sure you reference the original source. It is not your work and you may not pass it off as such. This script requires PHP5 as it makes use of the SimpleXML functions. As you can tell this script is really more of a utility script and not for general consumption. There are some comments thrown in but it is a very simple script so you should not have too much trouble with it.
There is some sample output from this script for download as well.
Geographic Calculations in PHP→
Recently I have been involved with a project that maps yachts during an ocean race, which got me thinking about basic calculations and conversions that would be useful to fellow developers. I envisage this being useful in projects leveraging Google or Yahoo maps. For the moment the class performs the following functions:
- Calculate the distance between two coordinate points on the earth’s surface (using Vincenty, Haversine, Great Circle or The Cosine Law)
- Conversion between units (metres to kilometres, nautical miles and miles).
- Convert coordinate notation (decimals to degrees, minutes & seconds and back again).
That is all you get for the moment, but it is pretty powerful for getting a “as the crow flies” distance between two coordinates. Vincenty’s formula is the most accurate method for calculation, but it is also the most processor intensive.
The attached file package contains an extensive set of “api” documentation and PHPDoc comments throughout the class to make customisation and use easier. I have also included a demo php file in there so you can see how it is intended to be used. It should also be pointed out here that this class requires some of the new OO features only available in PHP5, but it could easily be edited to be backwards compatible with PHP4.
Flickering Images→
I have been asked numerous times in the past how I create this effect in some forum signatures so now I am releasing the code so you can see. It could equally be used as a background image in CSS (like the photo of the excavator above) so you could have a circulating series of background or in this case header images. I am sure you can think of many other neat uses for this script.

refresh the page to see the above image change
Demo rar’d archive (includes demo ‘Disgraceful Alfa’ gallery images) can be found at the end of the article.
- Extract into a new directory
- Open image.jpg in your favourite editor
- Change the BASE_DIR to reflect the absolute path to the directory containing the image.php file eg. /home/simon/flickering/
- chmod the storage.dat file to 777
- Then open the url in your browser….enjoy!
This is the PHP script (image.jpg is the file name):
You also need to add a line to .htaccess:
ForceType application/x-httpd-php
Due to the change in the .htaccess file Apache now treats .jpg files as PHP in this directory. This then allows us to execute some php code which ultimately outputs a JPEG header and JPEG data, which shows up as an image when its linked to. The browser is none the wiser as it does not look like remote script invocation like some previous methods that have been released. So this can be used anywhere an image can without anyone knowing until they see it changing!
ADODB→
During a recent project I ended up using ADODB and found it very effective. Especially the wrapper it places around PHPs sessions, it stores them in the DB instead of in the temp directory, which can be less secure. It also handles encryption of the session variables contents, but only using MD5 originally and I prefer to use SHA1. So I hacked the following to allow me to do so and I contributed it to ADODB.
adodb-encrypt-sha1.php (new file):
Add to crypt.inc.php:
This code has now been added to the stable release of ADODB.