ENGINE_293: some sql issues ENGINE-293
authorKrista Bennett <krista@pep-project.org>
Thu, 07 Dec 2017 01:13:36 +0100
branchENGINE-293
changeset 23065744a544ecd2
parent 2305 f617e0acae94
child 2307 c33ec7f88dd9
ENGINE_293: some sql issues
src/pEpEngine.c
     1.1 --- a/src/pEpEngine.c	Tue Dec 05 16:35:23 2017 +0100
     1.2 +++ b/src/pEpEngine.c	Thu Dec 07 01:13:36 2017 +0100
     1.3 @@ -248,6 +248,56 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +static int table_contains_column(PEP_SESSION session, const char* table_name,
     1.8 +                                                      const char* col_name) {
     1.9 +
    1.10 +
    1.11 +    if (!session || !table_name || !col_name)
    1.12 +        return -1;
    1.13 +        
    1.14 +    // Table names can't be SQL parameters, so we do it this way.
    1.15 +    
    1.16 +    // these two must be the same number.
    1.17 +    char sql_buf[500];
    1.18 +    const size_t max_q_len = 500;
    1.19 +    
    1.20 +    size_t t_size, c_size, q_size;
    1.21 +    
    1.22 +    const char* q1 = "SELECT "; // 7
    1.23 +    const char* q2 = " from "; // 6
    1.24 +    const char* q3 = ";";       // 1
    1.25 +    
    1.26 +    q_size = 14;
    1.27 +    t_size = strlen(table_name);
    1.28 +    c_size = strlen(col_name);
    1.29 +    
    1.30 +    size_t query_len = q_size + c_size + t_size + 1;
    1.31 +    
    1.32 +    if (query_len > max_q_len)
    1.33 +        return -1;
    1.34 +
    1.35 +    strlcpy(sql_buf, q1, max_q_len);
    1.36 +    strlcat(sql_buf, col_name, max_q_len);
    1.37 +    strlcat(sql_buf, q2, max_q_len);
    1.38 +    strlcat(sql_buf, table_name, max_q_len);
    1.39 +    strlcat(sql_buf, q3, max_q_len);
    1.40 +
    1.41 +    sqlite3_stmt *stmt; 
    1.42 +
    1.43 +    sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
    1.44 +
    1.45 +    int retval = 0;
    1.46 +
    1.47 +    int rc = sqlite3_step(stmt);  
    1.48 +    if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
    1.49 +        retval = 1;
    1.50 +    }
    1.51 +
    1.52 +    sqlite3_finalize(stmt);      
    1.53 +        
    1.54 +    return retval;
    1.55 +}
    1.56 +
    1.57  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
    1.58  {
    1.59      PEP_STATUS status = PEP_STATUS_OK;
    1.60 @@ -476,6 +526,48 @@
    1.61              NULL,
    1.62              NULL);
    1.63          assert(int_result == SQLITE_OK);
    1.64 +        
    1.65 +        // Sometimes the user_version wasn't set correctly. Check to see if this
    1.66 +        // is really necessary...
    1.67 +        if (version == 1) {
    1.68 +            bool version_changed = true;
    1.69 +            
    1.70 +            if (table_contains_column(_session, "identity", "is_own") > 0) {
    1.71 +                version = 6;
    1.72 +            }
    1.73 +            else if (table_contains_column(_session, "sequences", "own") > 0) {
    1.74 +                version = 3;
    1.75 +            }
    1.76 +            else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
    1.77 +                version = 2;
    1.78 +            }
    1.79 +            else {
    1.80 +                version_changed = false;
    1.81 +            }
    1.82 +            
    1.83 +            if (version_changed) {
    1.84 +                // set it in the DB, finally. Yeesh.
    1.85 +                char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon. 
    1.86 +                sprintf(verbuf,"%d",version);
    1.87 +                
    1.88 +                size_t query_size = strlen(verbuf) + 25;
    1.89 +                char* query = calloc(query_size, 1);
    1.90 +                
    1.91 +                strlcpy(query, "pragma user_version = ", query_size);
    1.92 +                strlcat(query, verbuf, query_size);
    1.93 +                strlcat(query, ";", query_size);
    1.94 +                
    1.95 +                int_result = sqlite3_exec(
    1.96 +                    _session->db,
    1.97 +                    query,
    1.98 +                    user_version,
    1.99 +                    &version,
   1.100 +                    NULL
   1.101 +                );
   1.102 +                free(query);
   1.103 +            }
   1.104 +        }
   1.105 +
   1.106  
   1.107          if(version != 0) { 
   1.108              // Version has been already set
   1.109 @@ -496,7 +588,7 @@
   1.110              //     );
   1.111              //     assert(int_result == SQLITE_OK);
   1.112              // }
   1.113 -
   1.114 +            
   1.115              if (version < 2) {
   1.116                  int_result = sqlite3_exec(
   1.117                      _session->db,
   1.118 @@ -551,7 +643,7 @@
   1.119                      NULL,
   1.120                      NULL
   1.121                  );
   1.122 -                assert(int_result = SQLITE_OK);                
   1.123 +                assert(int_result == SQLITE_OK);                
   1.124                  int_result = sqlite3_exec(
   1.125                      _session->db,
   1.126                      "update identity\n"
   1.127 @@ -561,7 +653,7 @@
   1.128                      NULL,
   1.129                      NULL
   1.130                  );
   1.131 -                assert(int_result = SQLITE_OK);                
   1.132 +                assert(int_result == SQLITE_OK);                
   1.133              }
   1.134          }
   1.135          else {