-
Notifications
You must be signed in to change notification settings - Fork 6
/
sqlite3todot.c
191 lines (160 loc) · 5.68 KB
/
sqlite3todot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
sqlite3todot, originally by Gary "ChunkyKs" Briggs <[email protected]>
The license for this code is the same as that of sqlite
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
gcc -o sqlite3todot -lsqlite3 sqlite3todot.c
./sqlite3todot {dbname} [metadata db] | dot -Tpng -osomething.png
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlite3.h"
int main(int argc, char **argv) {
if(argc < 2 || 0 == strcmp(argv[1],"--help") || 0 == strcmp(argv[1], "-h")) {
fprintf(stderr, "Usage: %s <dbname> [metadb]\n", argv[0]);
exit(1);
}
sqlite3 *db = NULL;
int rc;
char *dbname = argv[1];
rc = sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READONLY, NULL);
if(SQLITE_OK != rc) {
fprintf(stderr, "Can't open database %s (%i): %s\n", dbname, rc, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
int have_meta = 0;
if(argc > 2) {
char attach_sql[1024];
char *exec_errmsg;
snprintf(attach_sql,sizeof(attach_sql),"ATTACH \"%s\" AS meta", argv[2]);
rc = sqlite3_exec(db, attach_sql, NULL, NULL, &exec_errmsg);
if(SQLITE_OK != rc) {
fprintf(stderr, "Error attaching meta db (%i): %s\n", rc, exec_errmsg);
sqlite3_free(exec_errmsg);
sqlite3_close(db);
exit(1);
}
have_meta = 1;
}
const char *tbl_list_sql = "SELECT tbl_name,NULL AS label,NULL AS color,NULL AS clusterid "
"FROM sqlite_master WHERE type='table'";
if(have_meta) {
tbl_list_sql = "SELECT sqlite_master.tbl_name AS tbl_name, meta.cluster.label AS label, meta.cluster.color AS color, meta.cluster.clusterid AS clusterid \n"
"FROM sqlite_master \n"
"LEFT JOIN meta.tbl_cluster ON sqlite_master.tbl_name=meta.tbl_cluster.tbl_name \n"
"LEFT JOIN meta.cluster ON meta.tbl_cluster.clusterid=meta.cluster.clusterid \n"
"LEFT JOIN meta.ignorelist ON sqlite_master.tbl_name=meta.ignorelist.tbl_name \n"
"WHERE meta.ignorelist.tbl_name IS NULL \n"
" AND main.sqlite_master.type='table'\n"
"GROUP BY sqlite_master.tbl_name\n"
"ORDER BY meta.cluster.clusterid\n";
}
sqlite3_stmt *tbl_list_stmt;
// fprintf(stderr, "%s\n", tbl_list_sql);
rc = sqlite3_prepare_v2(db, tbl_list_sql, -1, &tbl_list_stmt, NULL);
if(SQLITE_OK != rc) {
fprintf(stderr, "Can't prepare table list statement (%i): %s\n", rc, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
printf("digraph sqliteschema {\n");
printf("node [shape=plaintext];\n");
if(have_meta) {
const char settings_sql[] = "SELECT setting FROM meta.graphsettings";
sqlite3_stmt *settings_stmt;
rc = sqlite3_prepare_v2(db, settings_sql, -1, &settings_stmt, NULL);
if(SQLITE_OK != rc) {
fprintf(stderr, "Warning: Cannot find meta.graphsettings (%i): %s\n", rc, sqlite3_errmsg(db));
} else {
while(SQLITE_ROW == (rc = sqlite3_step(settings_stmt))) {
printf("%s\n", sqlite3_column_text(settings_stmt,0));
}
sqlite3_finalize(settings_stmt);
}
} else {
printf("rankdir=LR\n");
printf("splines=true\n");
printf("overlap=scale\n");
}
const int cols = 4;
int curr_cluster = -1;
while(SQLITE_ROW == (rc = sqlite3_step(tbl_list_stmt))) {
const char *tbl_name = (char *)sqlite3_column_text(tbl_list_stmt, 0);
int cluster_id = SQLITE_NULL==sqlite3_column_type(tbl_list_stmt,3)?-1:sqlite3_column_int(tbl_list_stmt,3);
if(cluster_id != curr_cluster && curr_cluster != -1) {
printf("}\n");
}
if(cluster_id != curr_cluster && cluster_id != -1) {
printf("subgraph cluster_%i {\n", cluster_id);
if(SQLITE_NULL!=sqlite3_column_type(tbl_list_stmt,1)) {
printf("label=\"%s\"\n", sqlite3_column_text(tbl_list_stmt,1));
}
if(SQLITE_NULL!=sqlite3_column_type(tbl_list_stmt,2)) {
printf("color=\"%s\"\n", sqlite3_column_text(tbl_list_stmt,2));
}
}
curr_cluster=cluster_id;
char *tbl_info_sql = sqlite3_mprintf("PRAGMA table_info(%q)", tbl_name);
sqlite3_stmt *tbl_info_stmt;
rc = sqlite3_prepare_v2(db, tbl_info_sql, -1, &tbl_info_stmt, NULL);
if(SQLITE_OK != rc) {
fprintf(stderr, "Can't prepare table info statement on table %s (%i): %s\n", tbl_name, rc, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
printf("%s [label=<<TABLE CELLSPACING=\"0\"><TR><TD COLSPAN=\"%i\"><U>%s</U></TD></TR>", tbl_name, cols, tbl_name);
int curr_row = 0;
int in_brace = 0;
while(SQLITE_ROW == (rc = sqlite3_step(tbl_info_stmt))) {
if(0 == curr_row%cols) {
in_brace = 1;
printf("<TR>");
}
printf("<TD PORT=\"%s\">%s</TD>",
sqlite3_column_text(tbl_info_stmt, 1),
sqlite3_column_text(tbl_info_stmt, 1));
curr_row++;
if(0 == curr_row%cols) {
in_brace = 0;
printf("</TR>");
}
}
if(in_brace) {
printf("</TR>");
}
printf("</TABLE>>];\n");
sqlite3_free(tbl_info_sql);
sqlite3_finalize(tbl_info_stmt);
}
if(curr_cluster != -1) {
printf("}\n");
}
sqlite3_reset(tbl_list_stmt);
while(SQLITE_ROW == (rc = sqlite3_step(tbl_list_stmt))) {
const char *tbl_name = (char *)sqlite3_column_text(tbl_list_stmt, 0);
char *fkey_info_sql = sqlite3_mprintf("PRAGMA foreign_key_list(%q)", tbl_name);
sqlite3_stmt *fkey_info_stmt;
rc = sqlite3_prepare_v2(db, fkey_info_sql, -1, &fkey_info_stmt, NULL);
if(SQLITE_OK != rc) {
fprintf(stderr, "Can't prepare foreign key statement on table %s (%i): %s\n", tbl_name, rc, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
while(SQLITE_ROW == (rc = sqlite3_step(fkey_info_stmt))) {
printf("%s:%s -> %s:%s;\n",
tbl_name,
sqlite3_column_text(fkey_info_stmt, 3),
sqlite3_column_text(fkey_info_stmt, 2),
sqlite3_column_text(fkey_info_stmt, 4));
}
sqlite3_free(fkey_info_sql);
sqlite3_finalize(fkey_info_stmt);
}
printf("}\n");
sqlite3_finalize(tbl_list_stmt);
sqlite3_close(db);
return 0;
}