| 6153 | } |
| 6154 | |
| 6155 | static int arch_packages(cbm_store_t *s, const char *project, const char *path, |
| 6156 | cbm_architecture_info_t *out) { |
| 6157 | char norm[CBM_SZ_512]; |
| 6158 | char like[CBM_SZ_512]; |
| 6159 | bool scoped = arch_path_prepare(path, norm, sizeof(norm), like, sizeof(like)); |
| 6160 | char sqlbuf[ST_SQL_BUF]; |
| 6161 | const char *base = "SELECT n.name, COUNT(*) as cnt FROM nodes n " |
| 6162 | "WHERE n.project=?1 AND n.label='Package'"; |
| 6163 | if (scoped) { |
| 6164 | snprintf(sqlbuf, sizeof(sqlbuf), |
| 6165 | "%s AND (n.file_path = ?2 OR n.file_path LIKE ?3) " |
| 6166 | "GROUP BY n.name ORDER BY cnt DESC LIMIT 15", |
| 6167 | base); |
| 6168 | } else { |
| 6169 | snprintf(sqlbuf, sizeof(sqlbuf), "%s GROUP BY n.name ORDER BY cnt DESC LIMIT 15", base); |
| 6170 | } |
| 6171 | sqlite3_stmt *stmt = NULL; |
| 6172 | if (sqlite3_prepare_v2(s->db, sqlbuf, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) { |
| 6173 | store_set_error_sqlite(s, "arch_packages"); |
| 6174 | return CBM_STORE_ERR; |
| 6175 | } |
| 6176 | bind_text(stmt, SKIP_ONE, project); |
| 6177 | if (scoped) { |
| 6178 | arch_bind_path_scope(stmt, ST_COL_2, ST_COL_3, norm, like); |
| 6179 | } |
| 6180 | |
| 6181 | int cap = ST_INIT_CAP_16; |
| 6182 | int n = 0; |
| 6183 | cbm_package_summary_t *arr = calloc(cap, sizeof(cbm_package_summary_t)); |
| 6184 | int scan_rc27; |
| 6185 | while ((scan_rc27 = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 6186 | if (n >= cap) { |
| 6187 | cap *= ST_GROWTH; |
| 6188 | arr = safe_realloc(arr, cap * sizeof(cbm_package_summary_t)); |
| 6189 | } |
| 6190 | arr[n].name = heap_strdup((const char *)sqlite3_column_text(stmt, 0)); |
| 6191 | arr[n].node_count = sqlite3_column_int(stmt, SKIP_ONE); |
| 6192 | n++; |
| 6193 | } |
| 6194 | if (scan_rc27 != SQLITE_DONE) { /* SCANCHK:27:stmt */ |
| 6195 | store_set_error_sqlite(s, "row scan aborted"); |
| 6196 | sqlite3_finalize(stmt); |
| 6197 | out->packages = arr; |
| 6198 | out->package_count = n; |
| 6199 | return CBM_STORE_ERR; |
| 6200 | } |
| 6201 | sqlite3_finalize(stmt); |
| 6202 | |
| 6203 | /* Fallback: group by QN segment if no Package nodes */ |
| 6204 | if (n == 0) { |
| 6205 | free(arr); |
| 6206 | int rc = arch_packages_from_qn(s, project, path, &arr, &n); |
| 6207 | if (rc != CBM_STORE_OK) { |
| 6208 | return rc; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | out->packages = arr; |
no test coverage detected