#include #include #include #define CHECK_RES(cmd, label) \ {\ int res = (cmd);\ if (res != 0) {\ fprintf(stderr, "Failed \"" #cmd "\" %d, \"%s\"\n", res, sqlite3_errstr(res));\ goto label;\ }\ } #define CHECK_EXPECT(cmd, code, label) \ {\ int res = (cmd);\ if (res != code) {\ fprintf(stderr, "Failed \"" #cmd "\" %d, \"%s\"\n", res, sqlite3_errstr(res));\ goto label;\ }\ } #define CHECK_END(cmd, label)\ {\ if (cmd + sizeof(cmd) -1 != tail) {\ fprintf(stderr, "Failed to run %s. Stopped at %s\n", cmd, tail); \ goto label;\ }\ } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "usage: %s [database.sqlite]\n", argv[0]); return -1; } const char* db_path = argv[1]; struct sqlite3 *db = NULL; CHECK_RES(sqlite3_open(db_path, &db), failure); struct sqlite3_stmt *stmt; const char* tail; // Create Comments if not exist const char cmd_query_table_by_name[] = "SELECT * " "FROM sqlite_master " "WHERE type='table' " "AND name='Comments'"; CHECK_RES(sqlite3_prepare_v2(db, cmd_query_table_by_name, sizeof(cmd_query_table_by_name), &stmt, &tail), cleanup); CHECK_END(cmd_query_table_by_name, cleanup); if (sqlite3_step(stmt) != SQLITE_ROW) { CHECK_RES(sqlite3_finalize(stmt), cleanup); const char cmd_create_table[] = "CREATE TABLE Comments(" "ID INTEGER PRIMARY KEY," "User VARCHAR(32)," "Comment VARCHAR(256)," "Time DATETIME" ");"; CHECK_RES(sqlite3_prepare(db, cmd_create_table, sizeof(cmd_create_table), &stmt, &tail), cleanup); CHECK_END(cmd_create_table, cleanup); CHECK_EXPECT(sqlite3_step(stmt), SQLITE_DONE, cleanup); } CHECK_RES(sqlite3_finalize(stmt), cleanup); const char cmd_last_comments[] = "SELECT User, Comment, Time " "FROM Comments " "WHERE ID IN (" "SELECT ID " "FROM Comments " "ORDER BY Time DESC " "LIMIT 10" ")" "ORDER BY Time ASC"; CHECK_RES(sqlite3_prepare_v2(db, cmd_last_comments, sizeof(cmd_last_comments), &stmt, &tail), cleanup); CHECK_END(cmd_last_comments, cleanup); while (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *username = sqlite3_column_text(stmt, 0); const unsigned char *comment = sqlite3_column_text(stmt, 1); const unsigned char *timestamp = sqlite3_column_text(stmt, 2); fprintf(stdout, "[%s] %s: %s\n", timestamp, username, comment); } CHECK_RES(sqlite3_finalize(stmt), cleanup); fflush(stdout); CHECK_RES(sqlite3_close(db), failure); fprintf(stdout, "Username: "); fflush(stdout); char username[32]; if (fgets(username, sizeof(username), stdin) != username) { fprintf(stderr, "Invalid input\n"); goto failure; } int username_len = strlen(username); username[username_len - 1] = '\0'; fprintf(stdout, "Comment: "); fflush(stdout); char comment[255]; if (fgets(comment, sizeof(comment), stdin) != comment) { fprintf(stderr, "Invalid input\n"); goto failure; } int comment_len = strlen(comment); comment[comment_len - 1] = '\0'; CHECK_RES(sqlite3_open(db_path, &db), failure); const char cmd_insert_comment[] = "INSERT INTO Comments(User, Comment, Time) " "VALUES (?, ?, CURRENT_TIMESTAMP)"; CHECK_RES(sqlite3_prepare_v2(db, cmd_insert_comment, sizeof(cmd_insert_comment), &stmt, &tail), cleanup); CHECK_END(cmd_insert_comment, cleanup); CHECK_RES(sqlite3_bind_text(stmt, 1, username, username_len, NULL), cleanup); CHECK_RES(sqlite3_bind_text(stmt, 2, comment, comment_len, NULL), cleanup); CHECK_EXPECT(sqlite3_step(stmt), SQLITE_DONE, cleanup); CHECK_RES(sqlite3_finalize(stmt), cleanup); fprintf(stdout, "Comment successfully posted\n"); cleanup: sqlite3_close(db); failure: return 0; }