| 6052 | } |
| 6053 | |
| 6054 | static int arch_packages(cbm_store_t *s, const char *project, const char *path, |
| 6055 | cbm_architecture_info_t *out) { |
| 6056 | char norm[CBM_SZ_512]; |
| 6057 | char like[CBM_SZ_512]; |
| 6058 | bool scoped = arch_path_prepare(path, norm, sizeof(norm), like, sizeof(like)); |
| 6059 | char sqlbuf[ST_SQL_BUF]; |
| 6060 | const char *base = "SELECT n.name, COUNT(*) as cnt FROM nodes n " |
| 6061 | "WHERE n.project=?1 AND n.label='Package'"; |
| 6062 | if (scoped) { |
| 6063 | snprintf(sqlbuf, sizeof(sqlbuf), |
| 6064 | "%s AND (n.file_path = ?2 OR n.file_path LIKE ?3) " |
| 6065 | "GROUP BY n.name ORDER BY cnt DESC LIMIT 15", |
| 6066 | base); |
| 6067 | } else { |
| 6068 | snprintf(sqlbuf, sizeof(sqlbuf), "%s GROUP BY n.name ORDER BY cnt DESC LIMIT 15", base); |
| 6069 | } |
| 6070 | sqlite3_stmt *stmt = NULL; |
| 6071 | if (sqlite3_prepare_v2(s->db, sqlbuf, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) { |
| 6072 | store_set_error_sqlite(s, "arch_packages"); |
| 6073 | return CBM_STORE_ERR; |
| 6074 | } |
| 6075 | bind_text(stmt, SKIP_ONE, project); |
| 6076 | if (scoped) { |
| 6077 | arch_bind_path_scope(stmt, ST_COL_2, ST_COL_3, norm, like); |
| 6078 | } |
| 6079 | |
| 6080 | int cap = ST_INIT_CAP_16; |
| 6081 | int n = 0; |
| 6082 | cbm_package_summary_t *arr = calloc(cap, sizeof(cbm_package_summary_t)); |
| 6083 | int scan_rc27; |
| 6084 | while ((scan_rc27 = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 6085 | if (n >= cap) { |
| 6086 | cap *= ST_GROWTH; |
| 6087 | arr = safe_realloc(arr, cap * sizeof(cbm_package_summary_t)); |
| 6088 | } |
| 6089 | arr[n].name = heap_strdup((const char *)sqlite3_column_text(stmt, 0)); |
| 6090 | arr[n].node_count = sqlite3_column_int(stmt, SKIP_ONE); |
| 6091 | n++; |
| 6092 | } |
| 6093 | if (scan_rc27 != SQLITE_DONE) { /* SCANCHK:27:stmt */ |
| 6094 | store_set_error_sqlite(s, "row scan aborted"); |
| 6095 | sqlite3_finalize(stmt); |
| 6096 | out->packages = arr; |
| 6097 | out->package_count = n; |
| 6098 | return CBM_STORE_ERR; |
| 6099 | } |
| 6100 | sqlite3_finalize(stmt); |
| 6101 | |
| 6102 | /* Fallback: group by QN segment if no Package nodes */ |
| 6103 | if (n == 0) { |
| 6104 | free(arr); |
| 6105 | int rc = arch_packages_from_qn(s, project, path, &arr, &n); |
| 6106 | if (rc != CBM_STORE_OK) { |
| 6107 | return rc; |
| 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | out->packages = arr; |
no test coverage detected