File: drive_sql.cc

package info (click to toggle)
lnav 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,808 kB
  • ctags: 3,343
  • sloc: cpp: 24,027; sh: 4,750; ansic: 4,541; makefile: 446; python: 165; sql: 56
file content (72 lines) | stat: -rw-r--r-- 1,716 bytes parent folder | download
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include <sqlite3.h>

#include "lnav.hh"
#include "auto_mem.hh"
#include "sqlite-extension-func.h"

struct callback_state {
    int cs_row;
};

struct _lnav_data lnav_data;

static int sql_callback(void *ptr,
                        int ncols,
                        char **colvalues,
                        char **colnames)
{
    struct callback_state *cs = (struct callback_state *)ptr;

    printf("Row %d:\n", cs->cs_row);
    for (int lpc = 0; lpc < ncols; lpc++) {
        printf("  Column %10s: %s\n", colnames[lpc], colvalues[lpc]);
    }

    cs->cs_row += 1;
    
    return 0;
}

int main(int argc, char *argv[])
{
    int retval = EXIT_SUCCESS;
    auto_mem<sqlite3> db(sqlite3_close);

    if (argc != 2) {
        fprintf(stderr, "error: expecting an SQL statement\n");
        retval = EXIT_FAILURE;
    }
    else if (sqlite3_open(":memory:", db.out()) != SQLITE_OK) {
        fprintf(stderr, "error: unable to make sqlite memory database\n");
        retval = EXIT_FAILURE;
    }
    else {
        auto_mem<char> errmsg(sqlite3_free);
        struct callback_state state;

        memset(&state, 0, sizeof(state));

        {
            int register_collation_functions(sqlite3 * db);

            register_sqlite_funcs(db.in(), sqlite_registration_funcs);
            register_collation_functions(db.in());
        }

        if (sqlite3_exec(db.in(),
            argv[1],
            sql_callback,
            &state,
            errmsg.out()) != SQLITE_OK) {
            fprintf(stderr, "error: sqlite3_exec failed -- %s\n", errmsg.in());
            retval = EXIT_FAILURE;
        }
    }

    return retval;
}