We have now properly set up the problem that we want to solve. We want a simple Swift API for constructing SQL statements that can be executed by SQLite. We would like this API to mimic what one would do when writing SQL directly, but should have the added benefit of better discoverability thanks to autocomplete, better safety thanks to static types, and the ability to reuse and compose queries together. And further we have given a preview of what this Swift API can look like by taking a peek at our Structured Queries library, which has not yet been released.
And that will come in due time, but for now let’s take a look at what it takes to build a query building library like this. It turns out to require the full power of nearly every feature available to modern Swift, including existentials, protocols with primary associated types, key paths, and even variadic generics. This library simply would not be possible to build in a version from Swift from just a year ago.
And along we the way we are also going to use this as an opportunity to dive deep into the concepts of SQL as its own language. It is a powerful language, and you will be a better engineer for being familiar with it.
We are going to start by defining a schema for some data types so that we can construct some interesting queries, and then see what it takes to build a library that can handle those queries.
Let’s begin…