Skip to content

Commit 9087e3d

Browse files
author
Pierre-Luc Gagné
committed
Add numeric default aliases and document tagged usage
1 parent 40bfff0 commit 9087e3d

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ struct product {
4848
// using price = tagged_column_field<price_tag, double>;
4949
// price price_;
5050
// // IMPORTANT: tag structs must be nested inside the table struct — not at global scope.
51+
//
52+
// // For wrapper types with default template parameters, use the provided aliases:
53+
// struct amount_tag {};
54+
// using amount = tagged_column_field<amount_tag, decimal_type_default>;
55+
// amount amount_;
5156

5257
// c) Fixed string literal — column name embedded directly, no tag needed.
5358
// ⚠ Two tables with identically-named and -typed columns share the same C++ type,
@@ -259,16 +264,27 @@ auto r2 = db.validate_database<my_db>();
259264
```cpp
260265
struct event {
261266
COLUMN_FIELD(id, uint32_t)
262-
COLUMN_FIELD(created_at, datetime_type) // DATETIME — defaults to NOW()
263-
COLUMN_FIELD(updated_at, timestamp_type) // TIMESTAMP — defaults to NOW()
264-
COLUMN_FIELD(duration, time_type) // TIME
265-
COLUMN_FIELD(started_at, std::optional<datetime_type>) // nullable DATETIME
267+
COLUMN_FIELD(created_at, datetime_type<>) // DATETIME — defaults to NOW()
268+
COLUMN_FIELD(updated_at, timestamp_type<>) // TIMESTAMP — defaults to NOW()
269+
COLUMN_FIELD(duration, time_type<>) // TIME
270+
COLUMN_FIELD(started_at, std::optional<datetime_type<>>) // nullable DATETIME
266271
};
267272

268273
// Fractional-second precision (0–6) is set on the value object:
269274
event row;
270-
row.created_at_ = datetime_type{tp, 3}; // DATETIME(3)
271-
row.duration_ = time_type{std::chrono::milliseconds{1500}, 3}; // TIME(3) = 00:00:01.500
275+
row.created_at_ = datetime_type<>{tp, 3}; // DATETIME(3)
276+
row.duration_ = time_type<>{std::chrono::milliseconds{1500}, 3}; // TIME(3) = 00:00:01.500
277+
278+
// Non-macro form with concise aliases:
279+
struct audit_event {
280+
struct created_at_tag {};
281+
using created_at = tagged_column_field<created_at_tag, datetime_type_default>;
282+
created_at created_at_;
283+
284+
struct amount_tag {};
285+
using amount = tagged_column_field<amount_tag, decimal_type_default>;
286+
amount amount_;
287+
};
272288
```
273289
274290
### Numeric Precision Overrides

lib/include/ds_mysql/numeric_field.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ struct decimal_type : detail::formatted_numeric_base<decimal_type<Precision, Sca
110110
}
111111
};
112112

113+
using float_type_default = float_type<>;
114+
using double_type_default = double_type<>;
115+
using decimal_type_default = decimal_type<>;
116+
113117
template <typename T>
114118
struct is_formatted_numeric_type : std::false_type {};
115119

tests/unit/test_ddl.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ struct tagged_temporal_alias_table {
111111
duration duration_;
112112
};
113113

114+
struct tagged_numeric_alias_table {
115+
struct id_tag {};
116+
using id = tagged_column_field<id_tag, uint32_t>;
117+
id id_;
118+
119+
struct value0_tag {};
120+
using value0 = tagged_column_field<value0_tag, float_type_default>;
121+
value0 value0_;
122+
123+
struct value1_tag {};
124+
using value1 = tagged_column_field<value1_tag, double_type_default>;
125+
value1 value1_;
126+
127+
struct value2_tag {};
128+
using value2 = tagged_column_field<value2_tag, decimal_type_default>;
129+
value2 value2_;
130+
};
131+
114132
struct symbol_with_indexes {
115133
COLUMN_FIELD(id, int32_t)
116134
COLUMN_FIELD(exchange_id, std::optional<int32_t>)
@@ -303,6 +321,22 @@ suite<"DDL"> ddl_suite = [] {
303321
<< sql;
304322
};
305323

324+
"tagged_column_field with numeric default aliases - emits FLOAT/DOUBLE/DECIMAL"_test = [] {
325+
expect(sql_type_for<float_type_default>() == "FLOAT"s);
326+
expect(sql_type_for<double_type_default>() == "DOUBLE"s);
327+
expect(sql_type_for<decimal_type_default>() == "DECIMAL"s);
328+
329+
auto const sql = create_table<tagged_numeric_alias_table>().build_sql();
330+
expect(sql ==
331+
"CREATE TABLE tagged_numeric_alias_table (\n"
332+
" id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,\n"
333+
" value0 FLOAT NOT NULL,\n"
334+
" value1 DOUBLE NOT NULL,\n"
335+
" value2 DECIMAL NOT NULL\n"
336+
");\n"s)
337+
<< sql;
338+
};
339+
306340
"create_table with numeric sql formatting options - emits FLOAT/DOUBLE/DECIMAL definitions"_test = [] {
307341
auto const sql = create_table<numeric_format_table>().build_sql();
308342
expect(sql ==

0 commit comments

Comments
 (0)