src/pEpEngine.c
branchENGINE-250
changeset 2479 aba55b53cd31
parent 2478 0460dfb7f3dc
child 2480 8799c08f67af
     1.1 --- a/src/pEpEngine.c	Tue Feb 06 18:48:45 2018 +0100
     1.2 +++ b/src/pEpEngine.c	Wed Feb 07 09:23:51 2018 +0100
     1.3 @@ -9,6 +9,7 @@
     1.4  
     1.5  #include <time.h>
     1.6  #include <stdlib.h>
     1.7 +#include <sqlite3.h>
     1.8  
     1.9  static volatile int init_count = -1;
    1.10  
    1.11 @@ -37,6 +38,32 @@
    1.12      }
    1.13  }
    1.14  
    1.15 +int sql_trace_callback (unsigned trace_constant, 
    1.16 +                        void* context_ptr,
    1.17 +                        void* P,
    1.18 +                        void* X) {
    1.19 +    switch (trace_constant) {
    1.20 +        case SQLITE_TRACE_STMT:
    1.21 +            fprintf(stderr, "SQL_DEBUG: STMT - ");
    1.22 +            const char* X_str = (const char*) X;
    1.23 +            if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
    1.24 +                fprintf(stderr, "%s\n", X_str);
    1.25 +            else
    1.26 +                fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
    1.27 +            break;
    1.28 +        case SQLITE_TRACE_ROW:
    1.29 +            fprintf(stderr, "SQL_DEBUG: ROW - ");
    1.30 +            fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
    1.31 +            break;            
    1.32 +        case SQLITE_TRACE_CLOSE:
    1.33 +            fprintf(stderr, "SQL_DEBUG: CLOSE - ");
    1.34 +            break;
    1.35 +        default:
    1.36 +            break;
    1.37 +    }
    1.38 +    return 0;
    1.39 +}
    1.40 +
    1.41  
    1.42  // sql manipulation statements
    1.43  static const char *sql_log = 
    1.44 @@ -98,7 +125,7 @@
    1.45  // Set person, but if already exist, only update.
    1.46  // if main_key_id already set, don't touch.
    1.47  static const char *sql_set_person = 
    1.48 -     "insert or replace into person (id, username, lang, main_key_id, device_group)"
    1.49 +     "insert into person (id, username, lang, main_key_id, device_group)"
    1.50       "  values (?1, ?2, ?3,"
    1.51  //     "    (select coalesce( "
    1.52  //     "          (select main_key_id from person where id = ?1), " 
    1.53 @@ -169,7 +196,7 @@
    1.54      "values (upper(replace(?1,' ',''))) ;";
    1.55  
    1.56  static const char *sql_set_identity_entry = 
    1.57 -    "insert or replace into identity ("
    1.58 +    "insert into identity ("
    1.59      "       address, main_key_id, "
    1.60      "       user_id, flags, is_own"
    1.61      "   ) values ("
    1.62 @@ -209,7 +236,7 @@
    1.63      "where address = ?2 and user_id = ?3 ;";
    1.64  
    1.65  static const char *sql_set_trust =
    1.66 -    "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
    1.67 +    "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
    1.68      "values (?1, upper(replace(?2,' ','')), ?3) ;";
    1.69  
    1.70  static const char *sql_update_trust =
    1.71 @@ -344,8 +371,8 @@
    1.72      "select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
    1.73  
    1.74  static const char *sql_add_userid_alias =
    1.75 -    "insert or replace into alternate_user_id (default_id, alternate_id) "
    1.76 -    "values (?1, ?2) ;";
    1.77 +    "insert or replace into alternate_user_id (alternate_id, default_id) "
    1.78 +    "values (?2, ?1) ;";
    1.79      
    1.80  static int user_version(void *_version, int count, char **text, char **name)
    1.81  {
    1.82 @@ -501,6 +528,11 @@
    1.83  
    1.84      sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
    1.85  
    1.86 +    sqlite3_trace_v2(_session->db, 
    1.87 +        SQLITE_TRACE_STMT | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
    1.88 +        sql_trace_callback,
    1.89 +        NULL);
    1.90 +
    1.91      assert(SYSTEM_DB);
    1.92      if (SYSTEM_DB == NULL) {
    1.93          status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
    1.94 @@ -1734,7 +1766,7 @@
    1.95            default_id[0] != '\0' && alias_id[0] != '\0'))
    1.96          return PEP_ILLEGAL_VALUE;
    1.97      
    1.98 -    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
    1.99 +    sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
   1.100  
   1.101      sqlite3_reset(session->add_userid_alias);
   1.102      sqlite3_bind_text(session->add_userid_alias, 1, default_id, -1,
   1.103 @@ -2063,12 +2095,12 @@
   1.104  PEP_STATUS set_or_update_with_identity(PEP_SESSION session,
   1.105                                         const pEp_identity* identity,
   1.106                                         PEP_STATUS (* set_function)(PEP_SESSION, const pEp_identity*, sqlite3_stmt*),
   1.107 +                                       sqlite3_stmt* update_query,
   1.108                                         sqlite3_stmt* set_query,
   1.109 -                                       sqlite3_stmt* update_query,
   1.110                                         bool guard_transaction) {
   1.111  
   1.112      if (guard_transaction) {
   1.113 -        sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   1.114 +        sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
   1.115      }                      
   1.116      PEP_STATUS status = set_function(session, identity, update_query);
   1.117      if (status != PEP_STATUS_OK) {
   1.118 @@ -2155,7 +2187,7 @@
   1.119              return PEP_KEY_BLACKLISTED;
   1.120      }
   1.121  
   1.122 -    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   1.123 +    sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
   1.124  
   1.125      if (identity->lang[0]) {
   1.126          assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
   1.127 @@ -3422,7 +3454,7 @@
   1.128      }
   1.129  
   1.130      if (*value) {
   1.131 -        sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   1.132 +        sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
   1.133          int32_t old_value = 0;
   1.134          status = _get_sequence_value(session, name, &old_value);
   1.135          if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
   1.136 @@ -3451,7 +3483,7 @@
   1.137      }
   1.138  
   1.139      assert(*value == 0);
   1.140 -    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   1.141 +    sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
   1.142      status = _increment_sequence_value(session, name, own);
   1.143      if (status == PEP_STATUS_OK) {
   1.144          status = _get_sequence_value(session, name, value);