| 3974 | /* ── Edge CRUD ──────────────────────────────────────────────────── */ |
| 3975 | |
| 3976 | int64_t cbm_store_insert_edge(cbm_store_t *s, const cbm_edge_t *e) { |
| 3977 | /* Conflict target includes local_name_gen (#768) so IMPORTS edges with |
| 3978 | * different local_name coexist while re-inserting the same import still |
| 3979 | * upserts. Must match the table's UNIQUE constraint in init_schema. */ |
| 3980 | sqlite3_stmt *stmt = |
| 3981 | prepare_cached(s, &s->stmt_insert_edge, |
| 3982 | "INSERT INTO edges (project, source_id, target_id, type, properties) " |
| 3983 | "VALUES (?1, ?2, ?3, ?4, ?5) " |
| 3984 | "ON CONFLICT(source_id, target_id, type, local_name_gen) DO UPDATE SET " |
| 3985 | "properties = json_patch(properties, ?5) " |
| 3986 | "RETURNING id;"); |
| 3987 | if (!stmt) { |
| 3988 | return CBM_STORE_ERR; |
| 3989 | } |
| 3990 | |
| 3991 | bind_text(stmt, SKIP_ONE, safe_str(e->project)); |
| 3992 | sqlite3_bind_int64(stmt, PAIR_LEN, e->source_id); |
| 3993 | sqlite3_bind_int64(stmt, CBM_SZ_3, e->target_id); |
| 3994 | bind_text(stmt, ST_COL_4, safe_str(e->type)); |
| 3995 | bind_text(stmt, ST_COL_5, safe_props(e->properties_json)); |
| 3996 | |
| 3997 | int rc = sqlite3_step(stmt); |
| 3998 | if (rc == SQLITE_ROW) { |
| 3999 | int64_t id = sqlite3_column_int64(stmt, 0); |
| 4000 | sqlite3_reset(stmt); /* unblock COMMIT — RETURNING leaves stmt active */ |
| 4001 | return id; |
| 4002 | } |
| 4003 | sqlite3_reset(stmt); |
| 4004 | store_set_error_sqlite(s, "insert_edge"); |
| 4005 | return CBM_STORE_ERR; |
| 4006 | } |
| 4007 | |
| 4008 | /* Scan an edge from current row of stmt. */ |
| 4009 | static void scan_edge(sqlite3_stmt *stmt, cbm_edge_t *e) { |