sequence
authorVolker Birk <vb@pep-project.org>
Mon, 16 May 2016 11:55:36 +0200
changeset 632a213fafe3f4f
parent 631 b2be2f799950
child 633 a2a4ad55e19e
sequence
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
test/pEpEngineTest.cc
     1.1 --- a/src/pEpEngine.c	Mon May 16 10:50:50 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon May 16 11:55:36 2016 +0200
     1.3 @@ -35,6 +35,8 @@
     1.4      static const char *sql_own_key_is_listed;
     1.5      static const char *sql_own_key_retrieve;
     1.6  
     1.7 +    static const char *sql_sequence_value;
     1.8 +
     1.9      bool in_first = false;
    1.10  
    1.11      assert(sqlite3_threadsafe());
    1.12 @@ -179,6 +181,11 @@
    1.13                  "       references pgp_keypair (fpr)\n"
    1.14                  "       on delete cascade\n"
    1.15                  ");\n"
    1.16 +                // sequences
    1.17 +                "create table if not exists sequences(\n"
    1.18 +                "   name text primary key,\n"
    1.19 +                "   value integer default 0\n"
    1.20 +                ");\n"
    1.21                  ,
    1.22              NULL,
    1.23              NULL,
    1.24 @@ -256,7 +263,12 @@
    1.25          sql_own_key_is_listed = "select count(*) from own_keys where fpr = upper(replace(?1,' ','')) ;";
    1.26  
    1.27          sql_own_key_retrieve = "select * from own_keys ;";
    1.28 -        
    1.29 + 
    1.30 +        sql_sequence_value = "insert or replace into sequences (name, value) "
    1.31 +                             "values (?1, "
    1.32 +                             "(select coalesce((select value + 1 from sequences "
    1.33 +                             "where name = ?1), 1 ))) ; "
    1.34 +                             "select value from sequences where name = ?1 ;";
    1.35      }
    1.36  
    1.37      int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
    1.38 @@ -716,10 +728,13 @@
    1.39      assert(session);
    1.40      assert(address);
    1.41      assert(address[0]);
    1.42 +    assert(identity);
    1.43  
    1.44 -    if (!(session && address && address[0]))
    1.45 +    if (!(session && address && address[0] && identity))
    1.46          return PEP_ILLEGAL_VALUE;
    1.47  
    1.48 +    *identity = NULL;
    1.49 +
    1.50      sqlite3_reset(session->get_identity);
    1.51      sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
    1.52      sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
    1.53 @@ -928,11 +943,11 @@
    1.54      assert(fpr);
    1.55      assert(comm_type);
    1.56  
    1.57 -    *comm_type = PEP_ct_unknown;
    1.58 -
    1.59      if (!(session && fpr && comm_type))
    1.60          return PEP_ILLEGAL_VALUE;
    1.61  
    1.62 +    *comm_type = PEP_ct_unknown;
    1.63 +
    1.64      sqlite3_reset(session->least_trust);
    1.65      sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
    1.66  
    1.67 @@ -1211,6 +1226,8 @@
    1.68              CRASHDUMP_MAX_LINES))
    1.69          return PEP_ILLEGAL_VALUE;
    1.70  
    1.71 +    *logdata = NULL;
    1.72 +
    1.73      int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
    1.74      const char *timestamp = NULL;
    1.75      const char *title = NULL;
    1.76 @@ -1291,6 +1308,8 @@
    1.77      if (!(session && languages))
    1.78          return PEP_ILLEGAL_VALUE;
    1.79  
    1.80 +    *languages = NULL;
    1.81 +
    1.82      const char *lang = NULL;
    1.83      const char *name = NULL;
    1.84      const char *phrase = NULL;
    1.85 @@ -1351,13 +1370,13 @@
    1.86      )
    1.87  {
    1.88      PEP_STATUS status = PEP_STATUS_OK;
    1.89 -    *phrase = NULL;
    1.90  
    1.91      assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
    1.92 -
    1.93      if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
    1.94          return PEP_ILLEGAL_VALUE;
    1.95  
    1.96 +    *phrase = NULL;
    1.97 +
    1.98      sqlite3_reset(session->i18n_token);
    1.99      sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
   1.100      sqlite3_bind_int(session->i18n_token, 2, phrase_id);
   1.101 @@ -1395,6 +1414,43 @@
   1.102      return status;
   1.103  }
   1.104  
   1.105 +DYNAMIC_API PEP_STATUS sequence_value(
   1.106 +        PEP_SESSION session,
   1.107 +        const char *name,
   1.108 +        int64_t *value
   1.109 +    )
   1.110 +{
   1.111 +    PEP_STATUS status = PEP_STATUS_OK;
   1.112 +    int result;
   1.113 +
   1.114 +    assert(session);
   1.115 +    assert(name);
   1.116 +    assert(value);
   1.117 +
   1.118 +    if (!(session && name && value))
   1.119 +        return PEP_ILLEGAL_VALUE;
   1.120 +
   1.121 +    *value = 0;
   1.122 +
   1.123 +    sqlite3_reset(session->sequence_value);
   1.124 +    sqlite3_bind_text(session->sequence_value, 1, name, -1, SQLITE_STATIC);
   1.125 +
   1.126 +    result = sqlite3_step(session->sequence_value);
   1.127 +    switch (result) {
   1.128 +        case SQLITE_ROW: {
   1.129 +            int64_t _value = (int64_t)
   1.130 +                    sqlite3_column_int64(session->sequence_value, 0);
   1.131 +            *value = _value;
   1.132 +            break;
   1.133 +        }
   1.134 +        default:
   1.135 +            status = PEP_CANNOT_FIND_IDENTITY;
   1.136 +    }
   1.137 +
   1.138 +    sqlite3_reset(session->sequence_value);
   1.139 +    return status;
   1.140 +}
   1.141 +
   1.142  DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
   1.143  {
   1.144      assert(session);
     2.1 --- a/src/pEpEngine.h	Mon May 16 10:50:50 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Mon May 16 11:55:36 2016 +0200
     2.3 @@ -766,6 +766,20 @@
     2.4      );
     2.5  
     2.6  
     2.7 +// sequence_value() - raise the value of a named sequence and retrieve it
     2.8 +//
     2.9 +//  parameters:
    2.10 +//      session (in)            session handle
    2.11 +//      name (in)               name of sequence
    2.12 +//      value (out)             value of sequence
    2.13 +
    2.14 +DYNAMIC_API PEP_STATUS sequence_value(
    2.15 +        PEP_SESSION session,
    2.16 +        const char *name,
    2.17 +        int64_t *value
    2.18 +    );
    2.19 +
    2.20 +
    2.21  DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session);
    2.22  
    2.23  #ifdef __cplusplus
     3.1 --- a/src/pEp_internal.h	Mon May 16 10:50:50 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Mon May 16 11:55:36 2016 +0200
     3.3 @@ -112,6 +112,9 @@
     3.4      sqlite3_stmt *own_key_is_listed;
     3.5      sqlite3_stmt *own_key_retrieve;
     3.6  
     3.7 +    // sequence value
     3.8 +    sqlite3_stmt *sequence_value;
     3.9 +
    3.10      // callbacks   
    3.11      examine_identity_t examine_identity;
    3.12      void *examine_management;
     4.1 --- a/test/pEpEngineTest.cc	Mon May 16 10:50:50 2016 +0200
     4.2 +++ b/test/pEpEngineTest.cc	Mon May 16 11:55:36 2016 +0200
     4.3 @@ -203,7 +203,7 @@
     4.4  	PEP_STATUS pep_set_result = set_identity(session, identity);
     4.5  	assert(pep_set_result == PEP_STATUS_OK);
     4.6      free_identity(identity);
     4.7 -	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
     4.8 +	get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
     4.9  	assert(identity);
    4.10  	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
    4.11