Guest User

c module modified

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