Skip to content

Commit e8cf0a3

Browse files
Pierre-Luc Gagnéclaude
andcommitted
feat: add col_expr operator== and operator!= overloads for named ID types
Allow col_ref comparisons against index_id, check_id, trigger_id, and other named ID types that expose a static name() method, avoiding the need to manually convert to std::string. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3873f80 commit e8cf0a3

4 files changed

Lines changed: 47 additions & 3 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+
- `col_expr` `operator==` and `operator!=` overloads for named ID types (`index_id`, `check_id`, `trigger_id`, etc.) — e.g. `col_ref(statistics::index_name{}) == index_id<"uq_symbol_ticker">{}`
14+
1115
---
1216

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

lib/include/ds_mysql/sql_core.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,23 @@ struct col_expr {
808808
[[nodiscard]] check_expr operator!=(value_type const& val) const {
809809
return not_equal<Col>(Col{val});
810810
}
811+
812+
template <typename T>
813+
requires(!std::is_convertible_v<T, value_type> &&
814+
requires {
815+
{ T::name() } -> std::convertible_to<std::string_view>;
816+
})
817+
[[nodiscard]] check_expr operator==(T const&) const {
818+
return equal<Col>(Col{value_type(T::name())});
819+
}
820+
template <typename T>
821+
requires(!std::is_convertible_v<T, value_type> &&
822+
requires {
823+
{ T::name() } -> std::convertible_to<std::string_view>;
824+
})
825+
[[nodiscard]] check_expr operator!=(T const&) const {
826+
return not_equal<Col>(Col{value_type(T::name())});
827+
}
811828
[[nodiscard]] check_expr operator<(value_type const& val) const {
812829
return less_than<Col>(Col{val});
813830
}

tests/unit/test_ddl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,9 +1671,8 @@ suite<"DDL Features"> ddl_features_suite = [] {
16711671
};
16721672

16731673
"create_index_on instance-based.unique - generates CREATE UNIQUE INDEX"_test = [] {
1674-
auto const sql = create_index_on(index_id<"uq_test_table_name">{}, test_table{}, test_table::name{})
1675-
.unique()
1676-
.build_sql();
1674+
auto const sql =
1675+
create_index_on(index_id<"uq_test_table_name">{}, test_table{}, test_table::name{}).unique().build_sql();
16771676
expect(sql == "CREATE UNIQUE INDEX uq_test_table_name ON test_table (name);\n"s) << sql;
16781677
};
16791678

tests/unit/test_dql.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,30 @@ suite<"DQL col_ref Operators"> dql_col_ref_suite = [] {
756756
<< sql;
757757
};
758758

759+
// -------------------------------------------------------------------
760+
// named ID types (index_id, check_id, trigger_id, etc.)
761+
// -------------------------------------------------------------------
762+
763+
"col_ref == index_id — generates equal WHERE SQL with name()"_test = [] {
764+
using statistics = mysql_metadata::information_schema::statistics;
765+
auto const sql = select(statistics::index_name{})
766+
.from(statistics{})
767+
.where(col_ref(statistics::index_name{}) == index_id<"uq_symbol_ticker">{})
768+
.build_sql();
769+
expect(sql == "SELECT index_name FROM information_schema.statistics WHERE index_name = 'uq_symbol_ticker'"s)
770+
<< sql;
771+
};
772+
773+
"col_ref != index_id — generates not_equal WHERE SQL with name()"_test = [] {
774+
using statistics = mysql_metadata::information_schema::statistics;
775+
auto const sql = select(statistics::index_name{})
776+
.from(statistics{})
777+
.where(col_ref(statistics::index_name{}) != index_id<"uq_symbol_ticker">{})
778+
.build_sql();
779+
expect(sql == "SELECT index_name FROM information_schema.statistics WHERE index_name != 'uq_symbol_ticker'"s)
780+
<< sql;
781+
};
782+
759783
// Without parens, & binds first: a | (b & c).
760784
// Verify the SQL differs so the test above is meaningful.
761785
"a | (b & c) — default precedence without parens"_test = [] {

0 commit comments

Comments
 (0)