Skip to content

Commit 6fa4dfb

Browse files
Pierre-Luc Gagnéclaude
andcommitted
feat: add instance-based overloads for table_constraint functions
Add instance-based overloads for primary_key, key, unique_key, fulltext_key, and spatial_key so columns can be passed as instances instead of template parameters: primary_key(col{}) key(index_id<"idx">{}, col{}) unique_key(index_id<"uq">{}, col1{}, col2{}) Template-only forms are preserved for backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0245863 commit 6fa4dfb

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Instance-based overloads for `table_constraint::primary_key`, `key`, `unique_key`, `fulltext_key`, `spatial_key` — e.g. `primary_key(col{})` and `key(index_id<"idx">{}, col{})` as alternatives to the template-only forms
14+
1115
---
1216

1317
## [4.0.0] – 2026-03-29

lib/include/ds_mysql/sql_ddl.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,60 @@ template <ColumnDescriptor... Cols>
116116
return "PRIMARY KEY " + columns_sql<Cols...>();
117117
}
118118

119+
template <ColumnDescriptor... Cols>
120+
requires(sizeof...(Cols) > 0)
121+
[[nodiscard]] inline std::string primary_key(Cols const&...) {
122+
return "PRIMARY KEY " + columns_sql<Cols...>();
123+
}
124+
119125
template <ColumnDescriptor... Cols, fixed_string Name>
120126
requires(sizeof...(Cols) > 0)
121127
[[nodiscard]] inline std::string key(index_id<Name> const&) {
122128
return "KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
123129
}
124130

131+
template <fixed_string Name, ColumnDescriptor... Cols>
132+
requires(sizeof...(Cols) > 0)
133+
[[nodiscard]] inline std::string key(index_id<Name> const&, Cols const&...) {
134+
return "KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
135+
}
136+
125137
template <ColumnDescriptor... Cols, fixed_string Name>
126138
requires(sizeof...(Cols) > 0)
127139
[[nodiscard]] inline std::string unique_key(index_id<Name> const&) {
128140
return "UNIQUE KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
129141
}
130142

143+
template <fixed_string Name, ColumnDescriptor... Cols>
144+
requires(sizeof...(Cols) > 0)
145+
[[nodiscard]] inline std::string unique_key(index_id<Name> const&, Cols const&...) {
146+
return "UNIQUE KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
147+
}
148+
131149
template <ColumnDescriptor... Cols, fixed_string Name>
132150
requires(sizeof...(Cols) > 0)
133151
[[nodiscard]] inline std::string fulltext_key(index_id<Name> const&) {
134152
return "FULLTEXT KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
135153
}
136154

155+
template <fixed_string Name, ColumnDescriptor... Cols>
156+
requires(sizeof...(Cols) > 0)
157+
[[nodiscard]] inline std::string fulltext_key(index_id<Name> const&, Cols const&...) {
158+
return "FULLTEXT KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
159+
}
160+
137161
template <ColumnDescriptor... Cols, fixed_string Name>
138162
requires(sizeof...(Cols) > 0)
139163
[[nodiscard]] inline std::string spatial_key(index_id<Name> const&) {
140164
return "SPATIAL KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
141165
}
142166

167+
template <fixed_string Name, ColumnDescriptor... Cols>
168+
requires(sizeof...(Cols) > 0)
169+
[[nodiscard]] inline std::string spatial_key(index_id<Name> const&, Cols const&...) {
170+
return "SPATIAL KEY " + std::string(Name) + ' ' + columns_sql<Cols...>();
171+
}
172+
143173
// Accepts a check_expr predicate; column name, operator, and value are all
144174
// derived from the typed predicate.
145175
//

tests/unit/test_ddl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ template <>
241241
struct ds_mysql::table_constraints<symbol_with_indexes> {
242242
static std::vector<std::string> get() {
243243
return {
244-
table_constraint::primary_key<symbol_with_indexes::id>(),
245-
table_constraint::key<symbol_with_indexes::exchange_id>(index_id<"index_exchange_id">{}),
244+
table_constraint::primary_key(symbol_with_indexes::id{}),
245+
table_constraint::key(index_id<"index_exchange_id">{}, symbol_with_indexes::exchange_id{}),
246246
};
247247
}
248248
};
@@ -265,7 +265,7 @@ template <>
265265
struct ds_mysql::table_constraints<audit_log> {
266266
static std::vector<std::string> get() {
267267
return {
268-
table_constraint::primary_key<audit_log::id>(),
268+
table_constraint::primary_key(audit_log::id{}),
269269
};
270270
}
271271
};
@@ -753,11 +753,11 @@ suite<"DDL"> ddl_suite = [] {
753753
};
754754

755755
"table_constraint helper SQL fragments - renders UNIQUE/FULLTEXT/SPATIAL"_test = [] {
756-
expect(table_constraint::unique_key<test_table::name>(index_id<"uq_test_name">{}) ==
756+
expect(table_constraint::unique_key(index_id<"uq_test_name">{}, test_table::name{}) ==
757757
"UNIQUE KEY uq_test_name (name)"s);
758-
expect(table_constraint::fulltext_key<test_table::name>(index_id<"ft_test_name">{}) ==
758+
expect(table_constraint::fulltext_key(index_id<"ft_test_name">{}, test_table::name{}) ==
759759
"FULLTEXT KEY ft_test_name (name)"s);
760-
expect(table_constraint::spatial_key<test_table::id>(index_id<"sp_test_id">{}) ==
760+
expect(table_constraint::spatial_key(index_id<"sp_test_id">{}, test_table::id{}) ==
761761
"SPATIAL KEY sp_test_id (id)"s);
762762
};
763763

0 commit comments

Comments
 (0)