Statistics
| Branch: | Tag: | Revision:

mongoose / examples / api_server / db_plugin_sqlite.c @ eaef5bd1

History | View | Annotate | Download (3.66 KB)

1
/*
2
 * Copyright (c) 2014 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "db_plugin.h"
7
#include "sqlite3.h"
8

    
9
void *db_open(const char *db_path) {
10
  sqlite3 *db = NULL;
11
  if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
12
                                        SQLITE_OPEN_FULLMUTEX,
13
                      NULL) == SQLITE_OK) {
14
    sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS kv(key PRIMARY KEY, val);", 0,
15
                 0, 0);
16
  }
17
  return db;
18
}
19

    
20
void db_close(void **db_handle) {
21
  if (db_handle != NULL && *db_handle != NULL) {
22
    sqlite3_close(*db_handle);
23
    *db_handle = NULL;
24
  }
25
}
26

    
27
static void op_set(struct mg_connection *nc, const struct http_message *hm,
28
                   const struct mg_str *key, void *db) {
29
  sqlite3_stmt *stmt = NULL;
30
  char value[200];
31
  const struct mg_str *body =
32
      hm->query_string.len > 0 ? &hm->query_string : &hm->body;
33

    
34
  mg_get_http_var(body, "value", value, sizeof(value));
35
  if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO kv VALUES (?, ?);", -1,
36
                         &stmt, NULL) == SQLITE_OK) {
37
    sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
38
    sqlite3_bind_text(stmt, 2, value, strlen(value), SQLITE_STATIC);
39
    sqlite3_step(stmt);
40
    sqlite3_finalize(stmt);
41
  }
42
  mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
43
}
44

    
45
static void op_get(struct mg_connection *nc, const struct http_message *hm,
46
                   const struct mg_str *key, void *db) {
47
  sqlite3_stmt *stmt = NULL;
48
  const char *data = NULL;
49
  int result;
50
  (void) hm;
51

    
52
  if (sqlite3_prepare_v2(db, "SELECT val FROM kv WHERE key = ?;", -1, &stmt,
53
                         NULL) == SQLITE_OK) {
54
    sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
55
    result = sqlite3_step(stmt);
56
    data = (char *) sqlite3_column_text(stmt, 0);
57
    if ((result == SQLITE_OK || result == SQLITE_ROW) && data != NULL) {
58
      mg_printf(nc,
59
                "HTTP/1.1 200 OK\r\n"
60
                "Content-Type: text/plain\r\n"
61
                "Content-Length: %d\r\n\r\n%s",
62
                (int) strlen(data), data);
63
    } else {
64
      mg_printf(nc, "%s",
65
                "HTTP/1.1 404 Not Found\r\n"
66
                "Content-Length: 0\r\n\r\n");
67
    }
68
    sqlite3_finalize(stmt);
69
  } else {
70
    mg_printf(nc, "%s",
71
              "HTTP/1.1 500 Server Error\r\n"
72
              "Content-Length: 0\r\n\r\n");
73
  }
74
}
75

    
76
static void op_del(struct mg_connection *nc, const struct http_message *hm,
77
                   const struct mg_str *key, void *db) {
78
  sqlite3_stmt *stmt = NULL;
79
  int result;
80
  (void) hm;
81

    
82
  if (sqlite3_prepare_v2(db, "DELETE FROM kv WHERE key = ?;", -1, &stmt,
83
                         NULL) == SQLITE_OK) {
84
    sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
85
    result = sqlite3_step(stmt);
86
    if (result == SQLITE_OK || result == SQLITE_ROW) {
87
      mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
88
    } else {
89
      mg_printf(nc, "%s",
90
                "HTTP/1.1 404 Not Found\r\n"
91
                "Content-Length: 0\r\n\r\n");
92
    }
93
    sqlite3_finalize(stmt);
94
  } else {
95
    mg_printf(nc, "%s",
96
              "HTTP/1.1 500 Server Error\r\n"
97
              "Content-Length: 0\r\n\r\n");
98
  }
99
}
100

    
101
void db_op(struct mg_connection *nc, const struct http_message *hm,
102
           const struct mg_str *key, void *db, int op) {
103
  switch (op) {
104
    case API_OP_GET:
105
      op_get(nc, hm, key, db);
106
      break;
107
    case API_OP_SET:
108
      op_set(nc, hm, key, db);
109
      break;
110
    case API_OP_DEL:
111
      op_del(nc, hm, key, db);
112
      break;
113
    default:
114
      mg_printf(nc, "%s",
115
                "HTTP/1.0 501 Not Implemented\r\n"
116
                "Content-Length: 0\r\n\r\n");
117
      break;
118
  }
119
}