4 #include <rikitiki/rikitiki>
5 #include <rikitiki/mongoose/mongoose>
6 #include <rikitiki/rest/rest>
7 #include <rikitiki/jsoncpp/jsoncpp>
8 #include <rikitiki/configuration/configuration>
11 using namespace rikitiki;
12 using namespace rikitiki::mongoose;
23 static const char* create_table,*select_all,*select,*insert;
24 sqlite3_stmt *insert_stmt, *select_all_stmt, *select_stmt;
29 if(retval != desired_retval){
31 ctx <<
"Sqlite3 err code: " << retval <<
"\n"
32 << sqlite3_errmsg(handle);
40 row[
"id"] = sqlite3_column_int(stmt, 0);
41 row[
"name"] = std::string((
const char*)sqlite3_column_text(stmt, 1));
42 row[
"author"] = std::string((
const char*)sqlite3_column_text(stmt, 2));
43 row[
"isbn"] = std::string((
const char*)sqlite3_column_text(stmt, 3));
51 std::string name = val[
"name"].asString();
52 std::string author = val[
"author"].asString();
53 std::string isbn = val[
"isbn"].asString();
55 sqlite3_reset(insert_stmt);
56 throwNQ(ctx, sqlite3_bind_text(insert_stmt, 1, &name[0], name.size(), SQLITE_TRANSIENT), SQLITE_OK);
57 throwNQ(ctx, sqlite3_bind_text(insert_stmt, 2, &author[0], author.size(), SQLITE_TRANSIENT), SQLITE_OK);
58 throwNQ(ctx, sqlite3_bind_text(insert_stmt, 3, &isbn[0], isbn.size(), SQLITE_TRANSIENT), SQLITE_OK);
59 throwNQ(ctx, sqlite3_step(insert_stmt), SQLITE_DONE);
60 ctx << sqlite3_last_insert_rowid(handle);
65 Json::Value val(Json::arrayValue);
68 sqlite3_reset(select_all_stmt);
70 while((retval = sqlite3_step(select_all_stmt)) == SQLITE_ROW)
71 val.append(readRow(select_all_stmt));
73 throwNQ(ctx, retval, SQLITE_DONE);
78 sqlite3_reset(select_stmt);
79 sqlite3_bind_int(select_stmt, 1,
id);
80 if(sqlite3_step(select_stmt) == SQLITE_ROW)
81 ctx << readRow(select_stmt);
88 sqlite3_exec(handle,
"DELETE from books", 0, 0, &error);
93 sqlite3_exec(handle,create_table,0,0,0);
98 sqlite3_exec(handle,create_table,0,0,0);
99 sqlite3_prepare_v2(handle, insert, strlen(insert), &insert_stmt, NULL);
100 sqlite3_prepare_v2(handle, select_all, strlen(select_all), &select_all_stmt, NULL);
101 sqlite3_prepare_v2(handle, select, strlen(select), &select_stmt, NULL);
105 int retval = sqlite3_open_v2(
":memory:", &handle, SQLITE_OPEN_READWRITE, 0);
107 LOG(Rest, Error) <<
"Sqlite3 err code: " << retval << std::endl << sqlite3_errmsg(handle) << std::endl;
113 void Register(
Server& server){
114 typedef RestModule T;
115 rikitiki::rest::Register(server,
"/book",
this);
119 const char* RestModule::insert =
"insert into books (name, author, isbn) values (?, ?, ?)";
120 const char* RestModule::create_table =
"create table if not exists books (id INTEGER PRIMARY KEY, name TEXT NOT NULL, author TEXT NOT NULL, isbn TEXT NOT NULL)";
121 const char* RestModule::select_all =
"select id, name, author, isbn from books;";
122 const char* RestModule::select =
"select id, name, author, isbn from books where id = ?;";