| 6263 | } |
| 6264 | |
| 6265 | static int arch_packages(cbm_store_t *s, const char *project, const char *path, |
| 6266 | cbm_architecture_info_t *out) { |
| 6267 | char norm[CBM_SZ_512]; |
| 6268 | char like[CBM_SZ_512]; |
| 6269 | bool scoped = arch_path_prepare(path, norm, sizeof(norm), like, sizeof(like)); |
| 6270 | char sqlbuf[ST_SQL_BUF]; |
| 6271 | const char *base = "SELECT n.name, COUNT(*) as cnt FROM nodes n " |
| 6272 | "WHERE n.project=?1 AND n.label='Package'"; |
| 6273 | if (scoped) { |
| 6274 | snprintf(sqlbuf, sizeof(sqlbuf), |
| 6275 | "%s AND (n.file_path = ?2 OR n.file_path LIKE ?3) " |
| 6276 | "GROUP BY n.name ORDER BY cnt DESC LIMIT 15", |
| 6277 | base); |
| 6278 | } else { |
| 6279 | snprintf(sqlbuf, sizeof(sqlbuf), "%s GROUP BY n.name ORDER BY cnt DESC LIMIT 15", base); |
| 6280 | } |
| 6281 | sqlite3_stmt *stmt = NULL; |
| 6282 | if (sqlite3_prepare_v2(s->db, sqlbuf, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) { |
| 6283 | store_set_error_sqlite(s, "arch_packages"); |
| 6284 | return CBM_STORE_ERR; |
| 6285 | } |
| 6286 | bind_text(stmt, SKIP_ONE, project); |
| 6287 | if (scoped) { |
| 6288 | arch_bind_path_scope(stmt, ST_COL_2, ST_COL_3, norm, like); |
| 6289 | } |
| 6290 | |
| 6291 | int cap = ST_INIT_CAP_16; |
| 6292 | int n = 0; |
| 6293 | cbm_package_summary_t *arr = calloc(cap, sizeof(cbm_package_summary_t)); |
| 6294 | int scan_rc27; |
| 6295 | while ((scan_rc27 = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 6296 | if (n >= cap) { |
| 6297 | cap *= ST_GROWTH; |
| 6298 | arr = safe_realloc(arr, cap * sizeof(cbm_package_summary_t)); |
| 6299 | } |
| 6300 | arr[n].name = heap_strdup((const char *)sqlite3_column_text(stmt, 0)); |
| 6301 | arr[n].node_count = sqlite3_column_int(stmt, SKIP_ONE); |
| 6302 | n++; |
| 6303 | } |
| 6304 | if (scan_rc27 != SQLITE_DONE) { /* SCANCHK:27:stmt */ |
| 6305 | store_set_error_sqlite(s, "row scan aborted"); |
| 6306 | sqlite3_finalize(stmt); |
| 6307 | out->packages = arr; |
| 6308 | out->package_count = n; |
| 6309 | return CBM_STORE_ERR; |
| 6310 | } |
| 6311 | sqlite3_finalize(stmt); |
| 6312 | |
| 6313 | /* Fallback: group by QN segment if no Package nodes */ |
| 6314 | if (n == 0) { |
| 6315 | free(arr); |
| 6316 | int rc = arch_packages_from_qn(s, project, path, &arr, &n); |
| 6317 | if (rc != CBM_STORE_OK) { |
| 6318 | return rc; |
| 6319 | } |
| 6320 | } |
| 6321 | |
| 6322 | out->packages = arr; |
no test coverage detected