PrevUpHomeNext

Building Expressions for Server-Side Evaluation

    const float max_radius = 100.0f;

    const exprn_mapper<float> x_distance = cinemas->location.x - my_place.x;
    const exprn_mapper<float> y_distance = cinemas->location.y - my_place.y;

    const predicate within_radius =
        x_distance*x_distance + y_distance*y_distance <= max_radius*max_radius;

Remarks

Here we construct three exprn_mapper objects: x_distance, y_distance, and within_radius. (Yes, within_radius is an exprn_mapper. The type predicate is a typedef of exprn_mapper<bool>.)

An exprn_mapper is an object that quince is able to translate into an SQL expression. As we shall see, the variety of contexts in which you can use exprn_mappers in quince is about the same as the variety of contexts in which you use SQL expressions in SQL. And the purpose is the same: to get computations done on the DBMS's side.

exprn_mapper's template parameter specifies its return type. That is the C++ equivalent of the SQL expression's return type, which has to be a single column. So you can have an exprn_mapper<float>, but you can't have an exprn_mapper<point>.

I've explained the exprn in exprn_mapper, but why mapper? That's because another job of an exprn_mapper is to convert computed results from a column format to a C++ type (although never the reverse). E.g. if quince were to execute the query cinemas.select(x_distance), it would rely on x_distance to convert the results.

exprn_mappers are built from:

-- by applying several devices, some of which we have already seen. The code above uses quince's overloads of -, *, and <=, which correspond to the obvious SQL operators. Earlier we used quince's upper() function, which corresponds to SQL's UPPER() function, and we used quince's count_all function within a scalar subquery (a somewhat advanced topic that I take up later).

We also defined a C++ function of our own, square(), which we then called (just like upper() etc.) in the context of building larger exprn_mappers. That wasn't a quince feature per se; it was just a C++ function call; but in its context it nicely suited the conceit of writing server-side expressions in C++ syntax. It was as though the DBMS had acquired a SQUARE() function, on par with UPPER().)


PrevUpHomeNext