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

Function doc_links_query

src/store/store.c:5313–5369  ·  view source on GitHub ↗

Run a row query (columns rel_path, line, syntax, raw, reason) with the * project bound to ?1 and an optional integer limit bound to ?2. */

Source from the content-addressed store, hash-verified

5311/* Run a row query (columns rel_path, line, syntax, raw, reason) with the
5312 * project bound to ?1 and an optional integer limit bound to ?2. */
5313static int doc_links_query(cbm_store_t *s, const char *sql, const char *project, int limit,
5314 cbm_doc_link_row_t **out, int *count) {
5315 sqlite3_stmt *stmt = NULL;
5316 if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
5317 store_set_error_sqlite(s, "doc_links get prepare");
5318 return CBM_STORE_ERR;
5319 }
5320 bind_text(stmt, SKIP_ONE, project);
5321 if (limit >= 0) {
5322 sqlite3_bind_int(stmt, ST_COL_2, limit);
5323 }
5324 int cap = ST_INIT_CAP_16;
5325 int n = 0;
5326 cbm_doc_link_row_t *arr = cbm_alloc(CBM_MEM_CLASS_STORE, (size_t)cap * sizeof(*arr));
5327 if (!arr) {
5328 sqlite3_finalize(stmt);
5329 return CBM_STORE_ERR;
5330 }
5331 int scan_rc;
5332 while ((scan_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
5333 if (n >= cap) {
5334 cbm_doc_link_row_t *grown =
5335 cbm_realloc(CBM_MEM_CLASS_STORE, arr, (size_t)cap * ST_GROWTH * sizeof(*arr));
5336 if (!grown) {
5337 sqlite3_finalize(stmt);
5338 cbm_store_free_doc_links(arr, n);
5339 return CBM_STORE_ERR;
5340 }
5341 arr = grown;
5342 cap *= ST_GROWTH;
5343 }
5344 cbm_doc_link_row_t *r = &arr[n];
5345 r->rel_path = doc_links_field_copy((const char *)sqlite3_column_text(stmt, 0), limit >= 0);
5346 r->line = sqlite3_column_int(stmt, SKIP_ONE);
5347 r->syntax =
5348 doc_links_field_copy((const char *)sqlite3_column_text(stmt, ST_COL_2), limit >= 0);
5349 r->raw =
5350 doc_links_field_copy((const char *)sqlite3_column_text(stmt, ST_COL_3), limit >= 0);
5351 r->reason =
5352 doc_links_field_copy((const char *)sqlite3_column_text(stmt, CBM_SZ_4), limit >= 0);
5353 n++;
5354 if (!r->rel_path || !r->syntax || !r->raw || !r->reason) {
5355 sqlite3_finalize(stmt);
5356 cbm_store_free_doc_links(arr, n);
5357 return CBM_STORE_ERR;
5358 }
5359 }
5360 sqlite3_finalize(stmt);
5361 if (scan_rc != SQLITE_DONE) {
5362 store_set_error_sqlite(s, "doc_links scan");
5363 cbm_store_free_doc_links(arr, n);
5364 return CBM_STORE_ERR;
5365 }
5366 *out = arr;
5367 *count = n;
5368 return CBM_STORE_OK;
5369}
5370

Callers 2

cbm_store_doc_links_getFunction · 0.85

Calls 6

store_set_error_sqliteFunction · 0.85
bind_textFunction · 0.85
cbm_allocFunction · 0.85
cbm_reallocFunction · 0.85
cbm_store_free_doc_linksFunction · 0.85
doc_links_field_copyFunction · 0.85

Tested by

no test coverage detected