MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / search_build_ranged_sql

Function search_build_ranged_sql

src/store/store.c:7287–7325  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7285}
7286
7287static bool search_build_ranged_sql(const char *select_cols, const char *where,
7288 const search_name_ranges_t *r, int limit, int offset,
7289 search_bind_t *binds, int *bind_idx, char **out_sql,
7290 char **out_count_sql) {
7291 if (*bind_idx + (r->count * ST_COL_2) > ST_SEARCH_MAX_BINDS) {
7292 return false;
7293 }
7294 size_t cap = (strlen(select_cols) + strlen(where) + ST_NAME_RANGE_ARM_SQL) * (size_t)r->count +
7295 ST_NAME_RANGE_ARM_SQL;
7296 char *sql = cbm_alloc(CBM_MEM_CLASS_STORE, cap);
7297 char *cnt = cbm_alloc(CBM_MEM_CLASS_STORE, cap);
7298 int first_bind = *bind_idx;
7299 size_t sl = 0;
7300 size_t cl = 0;
7301 bool ok = sql && cnt && search_sql_append(cnt, cap, &cl, "SELECT ");
7302 for (int i = 0; ok && i < r->count; i++) {
7303 int lo = *bind_idx + SKIP_ONE;
7304 where_bind_text(binds, bind_idx, r->lo[i]);
7305 where_bind_text(binds, bind_idx, r->hi[i]);
7306 ok = search_sql_append(sql, cap, &sl,
7307 "%s%s FROM nodes n WHERE (%s) AND n.name >= ?%d AND n.name < ?%d",
7308 i > 0 ? " UNION ALL " : "", select_cols, where, lo, lo + SKIP_ONE) &&
7309 search_sql_append(cnt, cap, &cl,
7310 "%s(SELECT COUNT(*) FROM nodes n WHERE (%s) AND n.name >= ?%d AND "
7311 "n.name < ?%d)",
7312 i > 0 ? " + " : "", where, lo, lo + SKIP_ONE);
7313 }
7314 ok = ok && search_sql_append(sql, cap, &sl, " ORDER BY 4, 1 LIMIT %d OFFSET %d", limit, offset);
7315 if (!ok) {
7316 /* Nothing is bound: the caller runs the plain query. */
7317 *bind_idx = first_bind;
7318 cbm_free(CBM_MEM_CLASS_STORE, sql);
7319 cbm_free(CBM_MEM_CLASS_STORE, cnt);
7320 return false;
7321 }
7322 *out_sql = sql;
7323 *out_count_sql = cnt;
7324 return true;
7325}
7326
7327int cbm_store_search(cbm_store_t *s, const cbm_search_params_t *params, cbm_search_output_t *out) {
7328 memset(out, 0, sizeof(*out));

Callers 1

cbm_store_searchFunction · 0.85

Calls 4

cbm_allocFunction · 0.85
search_sql_appendFunction · 0.85
where_bind_textFunction · 0.85
cbm_freeFunction · 0.85

Tested by

no test coverage detected