Monday, July 15, 2013

SQLBrite v1.0

I decided to use SQLite3 as the background storage for a small web service that I refactored, which led to SQLBrite, a short PHP class that wraps a SQLite3 object and defines various convenience methods, including automatic error checking, assertions, placeholder substitution, and callbacks. Despite its simplicity, it worked surprisingly well, and I found it provided a comfortably concise way of querying or modifying the database.

Perhaps the most interesting question was whether to write a bespoke method to substitute placeholders, or rely on statement objects to do that. For the sake of simplicity, I opted for the first possibility. Although currently this solution does not allow for specifying the types of the values, it made it possible to use SQLite3's querySingle(), and ensured that the placeholders used in the client code are properly supported. At the same time, it would be easy to extend SQLBrite, if necessary, to use statement objects and keep it backwards compatible. Some ideas on this are collected in the comments for the method responsible for placeholder substitution, sql().

SQLBrite is available from GitHub under GPL. Please get version 1.0 here: