Guest User

apache module with sqlite

a guest
Jul 13th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. /* Include the required headers from httpd */
  2. #include "httpd.h"
  3. #include "http_core.h"
  4. #include "http_protocol.h"
  5. #include "http_request.h"
  6. #include "uuid/uuid.h"
  7. #include "sqlite3.h"
  8.  
  9. /* Define prototypes of our functions in this module */
  10. static void register_hooks(apr_pool_t *pool);
  11. static int example_handler(request_rec *r);
  12. static int callback(void *NotUsed, int argc, char **argv, char **azColName);
  13.  
  14. /* Define our module as an entity and assign a function for registering hooks  */
  15. request_rec *rg = NULL;
  16. module AP_MODULE_DECLARE_DATA   example_module =
  17. {
  18.     STANDARD20_MODULE_STUFF,
  19.     NULL,            // Per-directory configuration handler
  20.     NULL,            // Merge handler for per-directory configurations
  21.     NULL,            // Per-server configuration handler
  22.     NULL,            // Merge handler for per-server configurations
  23.     NULL,            // Any directives we may have for httpd
  24.     register_hooks   // Our hook registering function
  25. };
  26.  
  27.  
  28. /* register_hooks: Adds a hook to the httpd process */
  29. static void register_hooks(apr_pool_t *pool)
  30. {
  31.    
  32.     /* Hook the request handler */
  33.     ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
  34. }
  35.  
  36. static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
  37.     int i;
  38.     printf("\n callback");
  39.     for (i = 0; i < argc; i++) {
  40.         //printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  41.         ap_rputs(azColName[i],rg);
  42.         ap_rputs(argv[i],rg);
  43.     }
  44.     printf("\n");
  45.     return 0;
  46. }
  47.  
  48. /* The handler function for our module.
  49.  * This is where all the fun happens!
  50.  */
  51.  
  52. static int example_handler(request_rec *r)
  53. {
  54.     /* First off, we need to check if this is a call for the "example" handler.
  55.      * If it is, we accept it and do our things, it not, we simply return DECLINED,
  56.      * and Apache will try somewhere else.
  57.      */
  58.     if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
  59.    
  60.     // The first thing we will do is write a simple "Hello, world!" back to the client.
  61.     ap_rputs("Hello, world!<br/>", r);
  62.      rg =r;
  63.     uuid_t uuid;
  64.    
  65.     // generate
  66.     uuid_generate_time_safe(uuid);
  67.    
  68.     // unparse (to string)
  69.     char uuid_str[37];      // ex. "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0"
  70.     uuid_unparse_lower(uuid, uuid_str);
  71.    // printf("generate uuid=%s\n", uuid_str);
  72.    
  73.     ap_rputs(uuid_str,r);
  74.    
  75.     int rc;
  76.     char *pStrSql;
  77.     sqlite3* db;
  78.     char *zErrMsg;
  79.     rc = sqlite3_open("a.db", &db);
  80.     if (rc) {
  81.         //        printf("\nCant open db ");
  82.          ap_rputs("\nCant open db",r);
  83.         exit(0);
  84.     } else {
  85.         //fprintf(stdout, "Opened database successfully\n");
  86.         ap_rputs("\nDatabase opened succesfully",r);
  87.     }
  88.    
  89.     pStrSql = "CREATE TABLE employee (name text,age int);";
  90.    
  91.     rc = sqlite3_exec(db, pStrSql, callback, 0, &zErrMsg);
  92.     if (rc != SQLITE_OK) {
  93.        
  94.         ap_rputs(zErrMsg,r);
  95.         sqlite3_free(zErrMsg);
  96.        
  97.     } else {
  98.         ap_rputs("\nTable created successfully",r);
  99.     }
  100.    
  101.     pStrSql = "INSERT INTO employee VALUES ('prakash',23);"\
  102.     "INSERT INTO employee VALUES ('dravid',41);" \
  103.     "INSERT INTO employee VALUES ('sourav',41);" \
  104.     "INSERT INTO employee VALUES ('sachin',40);"\
  105.     "INSERT INTO employee VALUES ('Some one',23);";
  106.    
  107.     rc = sqlite3_exec(db, pStrSql, callback, 0, &zErrMsg);
  108.     if (rc != SQLITE_OK) {
  109.          ap_rputs(zErrMsg,r);
  110.         sqlite3_free(zErrMsg);
  111.     } else {
  112.         ap_rputs("Records created succefulyy",r);
  113.     }
  114.    
  115.  
  116.    
  117.     return OK;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment