-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcli.c
93 lines (79 loc) · 2.18 KB
/
cli.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
#include "sqlite-lines.h"
#include <sqlite3.h>
#include <stdio.h>
#include <unistd.h>
// TODO
// 1. version
// 2. insert
// 3. --where, --groupby ?
// 4. --header?
// 5. CSV output proper?
// 6. JSON output?
// 7. --input or -i file
// 8. --dry-run flag
int main(int argc, char *argv[]) {
sqlite3 *db;
sqlite3_stmt *stmt;
int rc;
int opt;
if (argc < 1) {
fprintf(stderr, "USAGE: sqlite-lines select [where] [groupby] --header");
return 1;
}
rc = sqlite3_auto_extension((void (*)())sqlite3_lines_init);
if (rc != SQLITE_OK) {
fprintf(stderr, "Could not load sqlite3_lines_init: %s\n",
sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *file = "/dev/stdin";
rc = sqlite3_open(":memory:", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
sqlite3_str *query = sqlite3_str_new(db);
sqlite3_str_appendall(
query, "with base as (SELECT line, line as d from lines_read(?1))");
int argsCount = optind - argc;
sqlite3_str_appendf(query, "select %s", argv[optind]);
sqlite3_str_appendall(query, " from base");
if (argc > 2)
sqlite3_str_appendf(query, " where %s", argv[2]);
if (argc > 3)
sqlite3_str_appendf(query, " group by %s", argv[3]);
char *q = sqlite3_str_finish(query);
if (q == NULL) {
fprintf(stderr, "Cannot allocate memory\n");
sqlite3_close(db);
return 1;
}
rc = sqlite3_prepare_v2(db, q, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to fetch data: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = sqlite3_bind_text(stmt, 1, file, -1, SQLITE_TRANSIENT);
if (rc != SQLITE_OK) {
fprintf(stderr, "error binding: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
int cols = sqlite3_column_count(stmt);
// TODO handle errors, SQLITE_DONE, etc.
while (SQLITE_ROW == sqlite3_step(stmt)) {
for (int i = 0; i < cols; i++) {
printf("%s", sqlite3_column_text(stmt, i));
if (i != cols - 1)
printf(",");
}
printf("\n");
}
sqlite3_free(q);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}