Skip to content

Commit 1cf4012

Browse files
juliaschroederjengelh
authored andcommitted
mysql_adaptor: add user and domain comparison
Add comparison operator for sql_domain (based on domain name) and sql_user (based on displayname property).
1 parent 7dd9316 commit 1cf4012

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

exch/mysql_adaptor/sql2.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,33 @@ int mysql_adaptor_mbop_userlist(std::vector<sql_user> &out) try
751751
} catch (const std::bad_alloc &) {
752752
return ENOMEM;
753753
}
754+
755+
/**
756+
* @brief Compare domains based on (case-insensitive) domain name
757+
*
758+
* @param o Other domain
759+
*
760+
* @return Result of lexicographic comparison
761+
*/
762+
std::weak_ordering sql_domain::operator<=>(const sql_domain& o) const {
763+
auto r = strcasecmp(name.c_str(), o.name.c_str());
764+
return r < 0? std::weak_ordering::less : r == 0? std::weak_ordering::equivalent : std::weak_ordering::greater;
765+
}
766+
767+
/**
768+
* @brief Compare users based on (case-insensitive) display name
769+
*
770+
* Missing display names are substituted with the username.
771+
*
772+
* @param Other user
773+
*
774+
* @return Result of lexicographic comparison
775+
*/
776+
std::weak_ordering sql_user::operator<=>(const sql_user &o) const {
777+
auto i = propvals.find(PR_DISPLAY_NAME);
778+
auto name_this = i != propvals.end() ? i->second.c_str() : username.c_str();
779+
i = o.propvals.find(PR_DISPLAY_NAME);
780+
auto name_other = i != o.propvals.end() ? i->second.c_str() : o.username.c_str();
781+
auto r = strcasecmp(name_this, name_other);
782+
return r == 0? std::weak_ordering::equivalent : r < 0 ? std::weak_ordering::less : std::weak_ordering::greater;
783+
}

include/gromox/mysql_adaptor.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct mysql_adaptor_init_param {
5050

5151
struct sql_domain {
5252
std::string name, title, address;
53+
54+
std::weak_ordering operator<=>(const sql_domain&) const;
5355
};
5456

5557
/**
@@ -90,6 +92,8 @@ struct sql_user {
9092
std::string username, homeserver, maildir;
9193
std::vector<std::string> aliases; /* email addresses */
9294
std::map<unsigned int, std::string> propvals;
95+
96+
std::weak_ordering operator<=>(const sql_user &o) const;
9397
};
9498

9599
struct sql_group {

0 commit comments

Comments
 (0)