The diagram layer of a graph as sorted lines: its nodes, their containment, * the arrows, and the edges the elements' links resolved to. */
| 961 | /* The diagram layer of a graph as sorted lines: its nodes, their containment, |
| 962 | * the arrows, and the edges the elements' links resolved to. */ |
| 963 | static char *dg_state(const char *db) { |
| 964 | sqlite3 *h = NULL; |
| 965 | if (sqlite3_open_v2(db, &h, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) { |
| 966 | sqlite3_close(h); |
| 967 | return NULL; |
| 968 | } |
| 969 | const char *sql = |
| 970 | "SELECT 'N|' || label || '|' || qualified_name || '|' || name || '|' || " |
| 971 | "COALESCE(properties, '') AS l FROM nodes WHERE label IN ('Diagram', 'DiagramElement') " |
| 972 | "UNION ALL SELECT 'E|' || e.type || '|' || s.qualified_name || '|' || t.qualified_name || " |
| 973 | "'|' || COALESCE(e.properties, '') FROM edges e JOIN nodes s ON s.id = e.source_id " |
| 974 | "JOIN nodes t ON t.id = e.target_id WHERE e.type = 'DIAGRAM_ARROW' OR (e.type = " |
| 975 | "'DEFINES' AND t.label IN ('Diagram', 'DiagramElement')) OR (e.type = 'MENTIONS' AND " |
| 976 | "s.label IN ('Diagram', 'DiagramElement')) ORDER BY 1"; |
| 977 | sqlite3_stmt *st = NULL; |
| 978 | size_t cap = 4096; |
| 979 | size_t len = 0; |
| 980 | char *buf = malloc(cap); |
| 981 | buf[0] = '\0'; |
| 982 | if (sqlite3_prepare_v2(h, sql, -1, &st, NULL) == SQLITE_OK) { |
| 983 | while (sqlite3_step(st) == SQLITE_ROW) { |
| 984 | const char *line = (const char *)sqlite3_column_text(st, 0); |
| 985 | size_t l = strlen(line); |
| 986 | if (len + l + 2 > cap) { |
| 987 | cap = (len + l + 2) * 2; |
| 988 | buf = realloc(buf, cap); |
| 989 | } |
| 990 | memcpy(buf + len, line, l); |
| 991 | len += l; |
| 992 | buf[len++] = '\n'; |
| 993 | buf[len] = '\0'; |
| 994 | } |
| 995 | } |
| 996 | sqlite3_finalize(st); |
| 997 | sqlite3_close(h); |
| 998 | return buf; |
| 999 | } |
| 1000 | |
| 1001 | /* An incremental index of `repo` into inc_db, a full one into a fresh |
| 1002 | * full_db: the same diagram layer. 0, or -1 after printing the difference. */ |