|
| 1 | +[**\< Index**](/docs/README.md) |
| 2 | + |
| 3 | +# Names |
| 4 | + |
| 5 | +C++ and SQL use names to refer to entities. sqlpp23 automatically translates |
| 6 | +between names in C++ and names in SQL for columns and tables, e.g. when you |
| 7 | +select columns from a table, the resulting SQL expression contains the correct |
| 8 | +names and the members of result rows also refer to the correct names. |
| 9 | + |
| 10 | +```c++ |
| 11 | +for (const auto& row : db(select( |
| 12 | + foo.id, foo.name, foo.language).from(foo))) { |
| 13 | + // use row.id, row.name, row.language |
| 14 | +``` |
| 15 | +
|
| 16 | +This works because tables and their columns are specifically created such that |
| 17 | +they "know" their SQL name. |
| 18 | +
|
| 19 | +The C++ name of entities does not matter, though. |
| 20 | +
|
| 21 | +```c++ |
| 22 | +constexpr auto foo = TabFoo{}; // represented as tab_foo in SQL |
| 23 | +constexpr auto bar = foo; // also represented as tab_foo in SQL |
| 24 | +``` |
| 25 | + |
| 26 | +Also, other expressions, like function calls or arithmetic operations, for instance, do |
| 27 | +not have a name per se. |
| 28 | + |
| 29 | +If you want to give something an SQL name or if you want to change its SQL name, |
| 30 | +you can either |
| 31 | + |
| 32 | +- use the name of something that already has a name |
| 33 | +- prepare a name tag |
| 34 | + |
| 35 | +and attach that name tag to a table, column, or expression via the `.as()` |
| 36 | +function. |
| 37 | + |
| 38 | +## Name tags |
| 39 | + |
| 40 | +Name tags are created using the `SQLPP_CREATE_NAME_TAG` macro. It has to be used |
| 41 | +outside of functions (typically in an unnamed namespace), e.g. |
| 42 | + |
| 43 | +``` |
| 44 | +// Outside of function scope. |
| 45 | +SQLPP_CREATE_NAME_TAG(my_fancy_name); |
| 46 | +SQLPP_CREATE_NAME_TAG(max_id); |
| 47 | +``` |
| 48 | + |
| 49 | +sqlpp23 also provides a couple of convenience name tags in the `sqlpp::alias` |
| 50 | +namespace. These are |
| 51 | + |
| 52 | +``` |
| 53 | +sqlpp::alias::a; |
| 54 | +// ... |
| 55 | +sqlpp::alias::z; |
| 56 | +sqlpp::alias::left; |
| 57 | +sqlpp::alias::right; |
| 58 | +``` |
| 59 | + |
| 60 | +## `.as()` |
| 61 | + |
| 62 | +Tables, columns, and expressions in sqlpp23 expose the `.as()` member function. |
| 63 | +It takes name tag (see above) or an expression that already has a name as an |
| 64 | +argument and renames the in SQL using the `AS` operator. |
| 65 | + |
| 66 | +```c++ |
| 67 | +auto foo = TabFoo{}; // a table called tab_foo in SQL |
| 68 | +auto id = foo.id; // a column called id in SQL |
| 69 | +auto x = (foo.id + 17) * 4; // an sqlpp expression with no SQL name |
| 70 | +auto seven = 7; // a value with no SQL name |
| 71 | + |
| 72 | +// re-naming a table, e.g. for a self-join |
| 73 | +auto left = foo.as(sqlpp::alias::left); // tab_foo AS left |
| 74 | +auto right = foo.as(sqlpp::alias::right); // tab_foo AS right |
| 75 | + |
| 76 | +// re-naming a column |
| 77 | +id.as(sqlpp::alias::a); // tab_foo.id AS a |
| 78 | +left.id.as(my_fancy_name); // left.id AS my_fancy_name |
| 79 | + |
| 80 | +// naming an sqlpp expression |
| 81 | +x.as(foo.id); // ((tab_foo.id + 17) * 4) AS id |
| 82 | +max(foo.id).as(max_id); // MAX(tab_foo.id) AS max_id |
| 83 | + |
| 84 | +// naming a value |
| 85 | +sqlpp::value(seven).as(sqlpp::alias::s); // 7 AS s |
| 86 | +``` |
| 87 | +
|
| 88 | +## C++26 reflection (experimental) |
| 89 | +
|
| 90 | +C++26 offers [reflection](https://wg21.link/P2996). One aspect of that is an improved |
| 91 | +meta-programming access to names. |
| 92 | +
|
| 93 | +When used with C++26 and reflection support, the library offers another overload of the |
| 94 | +`.as` function. |
| 95 | +
|
| 96 | +```c++ |
| 97 | +// with reflection, you can use a string literal as template argument to name or |
| 98 | +// rename entities. |
| 99 | +foo.as<"left">(); // tab_foo AS left |
| 100 | +max(left.id).as<"max_id">(); // MAX(left.id) AS max_id |
| 101 | +``` |
| 102 | + |
| 103 | +**Experimental** |
| 104 | +As of this writing (2026-02), this is known to compile with g++-16.0.1 with `-freflection`. |
| 105 | + |
| 106 | +[**\< Index**](/docs/README.md) |
0 commit comments