(Disclaimer: what you are about to see is a glimpse of application code that uses quince, largely without explanation. A more detailed guided tour can be found here, and more detail still here.)
Consider the following task: print the titles of all the movies that are now playing on some screen in some cinema within a given radius of a given point.
If we use the following C++ data types:
struct point { float x; float y; }; struct movie { serial id; // primary key with automatically assigned values std::string title; }; struct cinema { serial id; // primary key with automatically assigned values point location; }; struct screen { serial id; // primary key with automatically assigned values serial cinema_id; // foreign key to cinemas table serial current_movie_id; // foreign key to movies table };
and we assume that the following have been defined elsewhere:
extern serial_table<movie> movies; extern serial_table<cinema> cinemas; extern serial_table<screen> screens; extern point my_place; extern float max_radius;
Then one way to construct a suitable query is:
const query<std::string> titles_playing_nearby = cinemas .where( (cinemas->location.x - my_place.x) * (cinemas->location.x - my_place.x) + (cinemas->location.y - my_place.y) * (cinemas->location.y - my_place.y) <= max_radius * max_radius ) .inner_join(screens, screens->cinema_id == cinemas->id) .inner_join(movies, movies->id == screens->current_movie_id) .select(movies->title) .distinct();
Now, to generate the required report, we use a C++11 for
-loop.
This causes the SQL query to be generated and executed, and the loop
body to be run as each row of output arrives:
for (const std::string &title: titles_playing_nearby) std::cout << title << std::endl;