Skip to content

Commit 950e38e

Browse files
committed
feat: Update client library API to use SORT syntax and enhance binlog tests
- Replace ORDER BY with SORT clause in client API (mygramclient.cpp/h) - Rename order_by/order_desc parameters to sort_column/sort_desc for clarity - Support MySQL-style LIMIT offset,count format when both are specified - Change default sort behavior to descending (sort_desc = true) - Update C API (mygramclient_c.cpp/h) to match new parameter names - Add comprehensive binlog parsing tests with real event structures - Add binlog reader unit tests for queue operations and event processing - Test GTID, TABLE_MAP, WRITE_ROWS, UPDATE_ROWS, DELETE_ROWS event parsing - Add multi-table routing and filter validation tests
1 parent 69265fc commit 950e38e

File tree

6 files changed

+680
-25
lines changed

6 files changed

+680
-25
lines changed

src/client/mygramclient.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class MygramClient::Impl {
234234
uint32_t offset, const std::vector<std::string>& and_terms,
235235
const std::vector<std::string>& not_terms,
236236
const std::vector<std::pair<std::string, std::string>>& filters,
237-
const std::string& order_by, bool order_desc) {
237+
const std::string& sort_column, bool sort_desc) {
238238
// Build command
239239
std::ostringstream cmd;
240240
cmd << "SEARCH " << table << " " << EscapeQueryString(query);
@@ -251,19 +251,28 @@ class MygramClient::Impl {
251251
cmd << " FILTER " << key << " = " << EscapeQueryString(value);
252252
}
253253

254-
if (!order_by.empty()) {
255-
cmd << " ORDER BY " << order_by << (order_desc ? " DESC" : " ASC");
256-
} else if (order_desc) {
257-
cmd << " ORDER DESC";
254+
// SORT clause (replaces ORDER BY)
255+
if (!sort_column.empty()) {
256+
cmd << " SORT " << sort_column << (sort_desc ? " DESC" : " ASC");
257+
} else if (!sort_desc) {
258+
// Only add SORT ASC if explicitly requesting ascending order for primary key
259+
cmd << " SORT ASC";
258260
}
261+
// Default is SORT DESC (primary key descending), so no need to add it explicitly
259262

260-
if (limit > 0) {
263+
// LIMIT clause - support MySQL-style offset,count format when both are specified
264+
if (limit > 0 && offset > 0) {
265+
cmd << " LIMIT " << offset << "," << limit;
266+
} else if (limit > 0) {
261267
cmd << " LIMIT " << limit;
262268
}
263269

264-
if (offset > 0) {
265-
cmd << " OFFSET " << offset;
266-
}
270+
// OFFSET clause - only needed if LIMIT didn't use offset,count format
271+
// (This is redundant if we used LIMIT offset,count above, but kept for clarity)
272+
// Note: The LIMIT offset,count format above already handles offset, so we skip this
273+
// if (offset > 0 && limit == 0) {
274+
// cmd << " OFFSET " << offset;
275+
// }
267276

268277
auto result = SendCommand(cmd.str());
269278
if (auto* err = std::get_if<Error>(&result)) {
@@ -653,8 +662,8 @@ bool MygramClient::IsConnected() const {
653662
std::variant<SearchResponse, Error> MygramClient::Search(
654663
const std::string& table, const std::string& query, uint32_t limit, uint32_t offset,
655664
const std::vector<std::string>& and_terms, const std::vector<std::string>& not_terms,
656-
const std::vector<std::pair<std::string, std::string>>& filters, const std::string& order_by, bool order_desc) {
657-
return impl_->Search(table, query, limit, offset, and_terms, not_terms, filters, order_by, order_desc);
665+
const std::vector<std::pair<std::string, std::string>>& filters, const std::string& sort_column, bool sort_desc) {
666+
return impl_->Search(table, query, limit, offset, and_terms, not_terms, filters, sort_column, sort_desc);
658667
}
659668

660669
std::variant<CountResponse, Error> MygramClient::Count(

src/client/mygramclient.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ class MygramClient {
197197
* @param and_terms Additional required terms
198198
* @param not_terms Excluded terms
199199
* @param filters Filter conditions (key=value pairs)
200-
* @param order_by Column name for ORDER BY (empty for default)
201-
* @param order_desc Sort descending (default: false = ascending)
200+
* @param sort_column Column name for SORT clause (empty for primary key)
201+
* @param sort_desc Sort descending (default: true = descending)
202202
* @return SearchResponse on success, Error on failure
203203
*/
204204
std::variant<SearchResponse, Error> Search(
@@ -207,8 +207,8 @@ class MygramClient {
207207
// - Default result limit
208208
uint32_t offset = 0, const std::vector<std::string>& and_terms = {},
209209
const std::vector<std::string>& not_terms = {},
210-
const std::vector<std::pair<std::string, std::string>>& filters = {}, const std::string& order_by = "",
211-
bool order_desc = false);
210+
const std::vector<std::pair<std::string, std::string>>& filters = {}, const std::string& sort_column = "",
211+
bool sort_desc = true);
212212

213213
/**
214214
* @brief Count matching documents

src/client/mygramclient_c.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ int mygramclient_is_connected(const MygramClient_C* client) {
120120
int mygramclient_search(MygramClient_C* client, const char* table, const char* query, uint32_t limit, uint32_t offset,
121121
MygramSearchResult_C** result) {
122122
return mygramclient_search_advanced(client, table, query, limit, offset, nullptr, 0, nullptr, 0, nullptr, nullptr, 0,
123-
nullptr, 0, result);
123+
nullptr, 1, result); // Default sort_desc = 1 (descending)
124124
}
125125

126126
int mygramclient_search_advanced(MygramClient_C* client, const char* table, const char* query, uint32_t limit,
127127
uint32_t offset, const char** and_terms, size_t and_count, const char** not_terms,
128128
size_t not_count, const char** filter_keys, const char** filter_values,
129-
size_t filter_count, const char* order_by, int order_desc,
129+
size_t filter_count, const char* sort_column, int sort_desc,
130130
MygramSearchResult_C** result) {
131131
if (client == nullptr || client->client == nullptr || table == nullptr || query == nullptr || result == nullptr) {
132132
return -1;
@@ -154,10 +154,10 @@ int mygramclient_search_advanced(MygramClient_C* client, const char* table, cons
154154
}
155155
}
156156

157-
std::string order_by_str = order_by != nullptr ? order_by : "";
157+
std::string sort_column_str = sort_column != nullptr ? sort_column : "";
158158

159159
auto search_result = client->client->Search(table, query, limit, offset, and_terms_vec, not_terms_vec, filters_vec,
160-
order_by_str, order_desc != 0);
160+
sort_column_str, sort_desc != 0);
161161

162162
if (auto* err = std::get_if<Error>(&search_result)) {
163163
client->last_error = err->message;

src/client/mygramclient_c.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ int mygramclient_search(MygramClient_C* client, const char* table, const char* q
138138
* @param filter_keys Array of filter keys (can be NULL)
139139
* @param filter_values Array of filter values (can be NULL)
140140
* @param filter_count Number of filters
141-
* @param order_by Column name for ORDER BY (can be NULL)
142-
* @param order_desc Sort descending (0 = ascending, 1 = descending)
141+
* @param sort_column Column name for SORT clause (can be NULL for primary key)
142+
* @param sort_desc Sort descending (0 = ascending, 1 = descending, default 1)
143143
* @param result Output search results (caller must free with mygramclient_free_search_result)
144144
* @return 0 on success, -1 on error
145145
*/
146146
int mygramclient_search_advanced(MygramClient_C* client, const char* table, const char* query, uint32_t limit,
147147
uint32_t offset, const char** and_terms, size_t and_count, const char** not_terms,
148148
size_t not_count, const char** filter_keys, const char** filter_values,
149-
size_t filter_count, const char* order_by, int order_desc,
149+
size_t filter_count, const char* sort_column, int sort_desc,
150150
MygramSearchResult_C** result);
151151

152152
/**

0 commit comments

Comments
 (0)