Simplify finalization of sqlite statements. sync
authorNeal H. Walfield <neal@pep.foundation>
Fri, 28 Dec 2018 12:40:23 +0100
branchsync
changeset 3212fb4124923112
parent 3211 8ca54e726b3c
child 3213 e56efa502f08
Simplify finalization of sqlite statements.

- Avoid boilerplate.
src/pEp_internal.h
src/pgp_sequoia.c
     1.1 --- a/src/pEp_internal.h	Wed Dec 26 20:48:02 2018 +0100
     1.2 +++ b/src/pEp_internal.h	Fri Dec 28 12:40:23 2018 +0100
     1.3 @@ -130,20 +130,22 @@
     1.4  #elif defined(USE_SEQUOIA)
     1.5      sq_context_t ctx;
     1.6      sqlite3 *key_db;
     1.7 -    sqlite3_stmt *begin_transaction;
     1.8 -    sqlite3_stmt *commit_transaction;
     1.9 -    sqlite3_stmt *rollback_transaction;
    1.10 -    sqlite3_stmt *tpk_find;
    1.11 -    sqlite3_stmt *tsk_find;
    1.12 -    sqlite3_stmt *tpk_find_by_keyid;
    1.13 -    sqlite3_stmt *tsk_find_by_keyid;
    1.14 -    sqlite3_stmt *tpk_find_by_email;
    1.15 -    sqlite3_stmt *tsk_find_by_email;
    1.16 -    sqlite3_stmt *tpk_all;
    1.17 -    sqlite3_stmt *tsk_all;
    1.18 -    sqlite3_stmt *tpk_save_insert_primary;
    1.19 -    sqlite3_stmt *tpk_save_insert_subkeys;
    1.20 -    sqlite3_stmt *tpk_save_insert_userids;
    1.21 +    struct {
    1.22 +        sqlite3_stmt *begin_transaction;
    1.23 +        sqlite3_stmt *commit_transaction;
    1.24 +        sqlite3_stmt *rollback_transaction;
    1.25 +        sqlite3_stmt *tpk_find;
    1.26 +        sqlite3_stmt *tsk_find;
    1.27 +        sqlite3_stmt *tpk_find_by_keyid;
    1.28 +        sqlite3_stmt *tsk_find_by_keyid;
    1.29 +        sqlite3_stmt *tpk_find_by_email;
    1.30 +        sqlite3_stmt *tsk_find_by_email;
    1.31 +        sqlite3_stmt *tpk_all;
    1.32 +        sqlite3_stmt *tsk_all;
    1.33 +        sqlite3_stmt *tpk_save_insert_primary;
    1.34 +        sqlite3_stmt *tpk_save_insert_subkeys;
    1.35 +        sqlite3_stmt *tpk_save_insert_userids;
    1.36 +    } sq_sql;
    1.37  #endif
    1.38  
    1.39      PEP_cryptotech_t *cryptotech;
     2.1 --- a/src/pgp_sequoia.c	Wed Dec 26 20:48:02 2018 +0100
     2.2 +++ b/src/pgp_sequoia.c	Fri Dec 28 12:40:23 2018 +0100
     2.3 @@ -166,31 +166,31 @@
     2.4  
     2.5      sqlite_result
     2.6          = sqlite3_prepare_v2(session->key_db, "begin transaction",
     2.7 -                             -1, &session->begin_transaction, NULL);
     2.8 +                             -1, &session->sq_sql.begin_transaction, NULL);
     2.9      assert(sqlite_result == SQLITE_OK);
    2.10  
    2.11      sqlite_result
    2.12          = sqlite3_prepare_v2(session->key_db, "commit transaction",
    2.13 -                             -1, &session->commit_transaction, NULL);
    2.14 +                             -1, &session->sq_sql.commit_transaction, NULL);
    2.15      assert(sqlite_result == SQLITE_OK);
    2.16  
    2.17      sqlite_result
    2.18          = sqlite3_prepare_v2(session->key_db, "rollback transaction",
    2.19 -                             -1, &session->rollback_transaction, NULL);
    2.20 +                             -1, &session->sq_sql.rollback_transaction, NULL);
    2.21      assert(sqlite_result == SQLITE_OK);
    2.22  
    2.23      sqlite_result
    2.24          = sqlite3_prepare_v2(session->key_db,
    2.25                               "SELECT tpk, secret FROM keys"
    2.26                               " WHERE primary_key == ?",
    2.27 -                             -1, &session->tpk_find, NULL);
    2.28 +                             -1, &session->sq_sql.tpk_find, NULL);
    2.29      assert(sqlite_result == SQLITE_OK);
    2.30  
    2.31      sqlite_result
    2.32          = sqlite3_prepare_v2(session->key_db,
    2.33                               "SELECT tpk, secret FROM keys"
    2.34                               " WHERE primary_key == ? and secret == 1",
    2.35 -                             -1, &session->tsk_find, NULL);
    2.36 +                             -1, &session->sq_sql.tsk_find, NULL);
    2.37      assert(sqlite_result == SQLITE_OK);
    2.38  
    2.39      sqlite_result
    2.40 @@ -199,7 +199,7 @@
    2.41                               " LEFT JOIN keys"
    2.42                               "  ON subkeys.primary_key == keys.primary_key"
    2.43                               " WHERE subkey == ?",
    2.44 -                             -1, &session->tpk_find_by_keyid, NULL);
    2.45 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
    2.46      assert(sqlite_result == SQLITE_OK);
    2.47  
    2.48      sqlite_result
    2.49 @@ -208,7 +208,7 @@
    2.50                               " LEFT JOIN keys"
    2.51                               "  ON subkeys.primary_key == keys.primary_key"
    2.52                               " WHERE subkey == ?",
    2.53 -                             -1, &session->tpk_find_by_keyid, NULL);
    2.54 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
    2.55      assert(sqlite_result == SQLITE_OK);
    2.56  
    2.57      sqlite_result
    2.58 @@ -217,7 +217,7 @@
    2.59                               " LEFT JOIN keys"
    2.60                               "  ON subkeys.primary_key == keys.primary_key"
    2.61                               " WHERE subkey == ? and keys.secret == 1",
    2.62 -                             -1, &session->tsk_find_by_keyid, NULL);
    2.63 +                             -1, &session->sq_sql.tsk_find_by_keyid, NULL);
    2.64      assert(sqlite_result == SQLITE_OK);
    2.65  
    2.66      sqlite_result
    2.67 @@ -226,7 +226,7 @@
    2.68                               " LEFT JOIN keys"
    2.69                               "  ON userids.primary_key == keys.primary_key"
    2.70                               " WHERE userid == ?",
    2.71 -                             -1, &session->tpk_find_by_email, NULL);
    2.72 +                             -1, &session->sq_sql.tpk_find_by_email, NULL);
    2.73      assert(sqlite_result == SQLITE_OK);
    2.74  
    2.75      sqlite_result
    2.76 @@ -235,19 +235,19 @@
    2.77                               " LEFT JOIN keys"
    2.78                               "  ON userids.primary_key == keys.primary_key"
    2.79                               " WHERE userid == ? and keys.secret == 1",
    2.80 -                             -1, &session->tsk_find_by_email, NULL);
    2.81 +                             -1, &session->sq_sql.tsk_find_by_email, NULL);
    2.82      assert(sqlite_result == SQLITE_OK);
    2.83  
    2.84      sqlite_result
    2.85          = sqlite3_prepare_v2(session->key_db,
    2.86                               "select tpk, secret from keys",
    2.87 -                             -1, &session->tpk_all, NULL);
    2.88 +                             -1, &session->sq_sql.tpk_all, NULL);
    2.89      assert(sqlite_result == SQLITE_OK);
    2.90  
    2.91      sqlite_result
    2.92          = sqlite3_prepare_v2(session->key_db,
    2.93                               "select tpk, secret from keys where secret = 1",
    2.94 -                             -1, &session->tsk_all, NULL);
    2.95 +                             -1, &session->sq_sql.tsk_all, NULL);
    2.96      assert(sqlite_result == SQLITE_OK);
    2.97  
    2.98      sqlite_result
    2.99 @@ -255,7 +255,7 @@
   2.100                               "INSERT OR REPLACE INTO keys"
   2.101                               "   (primary_key, secret, tpk)"
   2.102                               " VALUES (?, ?, ?)",
   2.103 -                             -1, &session->tpk_save_insert_primary, NULL);
   2.104 +                             -1, &session->sq_sql.tpk_save_insert_primary, NULL);
   2.105      assert(sqlite_result == SQLITE_OK);
   2.106  
   2.107      sqlite_result
   2.108 @@ -263,7 +263,7 @@
   2.109                               "INSERT OR REPLACE INTO subkeys"
   2.110                               "   (subkey, primary_key)"
   2.111                               " VALUES (?, ?)",
   2.112 -                             -1, &session->tpk_save_insert_subkeys, NULL);
   2.113 +                             -1, &session->sq_sql.tpk_save_insert_subkeys, NULL);
   2.114      assert(sqlite_result == SQLITE_OK);
   2.115  
   2.116      sqlite_result
   2.117 @@ -271,7 +271,7 @@
   2.118                               "INSERT OR REPLACE INTO userids"
   2.119                               "   (userid, primary_key)"
   2.120                               " VALUES (?, ?)",
   2.121 -                             -1, &session->tpk_save_insert_userids, NULL);
   2.122 +                             -1, &session->sq_sql.tpk_save_insert_userids, NULL);
   2.123      assert(sqlite_result == SQLITE_OK);
   2.124  
   2.125   out:
   2.126 @@ -282,53 +282,12 @@
   2.127  
   2.128  void pgp_release(PEP_SESSION session, bool out_last)
   2.129  {
   2.130 -    if (session->begin_transaction)
   2.131 -        sqlite3_finalize(session->begin_transaction);
   2.132 -    session->begin_transaction = NULL;
   2.133 -    if (session->commit_transaction)
   2.134 -        sqlite3_finalize(session->commit_transaction);
   2.135 -    session->commit_transaction = NULL;
   2.136 -    if (session->rollback_transaction)
   2.137 -        sqlite3_finalize(session->rollback_transaction);
   2.138 -    session->rollback_transaction = NULL;
   2.139 -
   2.140 -    if (session->tpk_find)
   2.141 -        sqlite3_finalize(session->tpk_find);
   2.142 -    session->tpk_find = NULL;
   2.143 -    if (session->tsk_find)
   2.144 -        sqlite3_finalize(session->tsk_find);
   2.145 -    session->tsk_find = NULL;
   2.146 -
   2.147 -    if (session->tpk_find_by_keyid)
   2.148 -        sqlite3_finalize(session->tpk_find_by_keyid);
   2.149 -    session->tpk_find_by_keyid = NULL;
   2.150 -    if (session->tsk_find_by_keyid)
   2.151 -        sqlite3_finalize(session->tsk_find_by_keyid);
   2.152 -    session->tsk_find_by_keyid = NULL;
   2.153 -
   2.154 -    if (session->tpk_find_by_email)
   2.155 -        sqlite3_finalize(session->tpk_find_by_email);
   2.156 -    session->tpk_find_by_email = NULL;
   2.157 -    if (session->tsk_find_by_email)
   2.158 -        sqlite3_finalize(session->tsk_find_by_email);
   2.159 -    session->tsk_find_by_email = NULL;
   2.160 -
   2.161 -    if (session->tpk_all)
   2.162 -        sqlite3_finalize(session->tpk_all);
   2.163 -    session->tpk_all = NULL;
   2.164 -    if (session->tsk_all)
   2.165 -        sqlite3_finalize(session->tsk_all);
   2.166 -    session->tsk_all = NULL;
   2.167 -
   2.168 -    if (session->tpk_save_insert_primary)
   2.169 -        sqlite3_finalize(session->tpk_save_insert_primary);
   2.170 -    session->tpk_save_insert_primary = NULL;
   2.171 -    if (session->tpk_save_insert_subkeys)
   2.172 -        sqlite3_finalize(session->tpk_save_insert_subkeys);
   2.173 -    session->tpk_save_insert_subkeys = NULL;
   2.174 -    if (session->tpk_save_insert_userids)
   2.175 -        sqlite3_finalize(session->tpk_save_insert_userids);
   2.176 -    session->tpk_save_insert_userids = NULL;
   2.177 +    sqlite3_stmt **stmts = (sqlite3_stmt **) &session->sq_sql;
   2.178 +    for (int i = 0; i < sizeof(session->sq_sql) / sizeof(*stmts); i ++)
   2.179 +        if (stmts[i]) {
   2.180 +            sqlite3_finalize(stmts[i]);
   2.181 +            stmts[i] = NULL;
   2.182 +        }
   2.183  
   2.184      if (session->key_db) {
   2.185          int result = sqlite3_close_v2(session->key_db);
   2.186 @@ -508,7 +467,7 @@
   2.187  
   2.188      T("(%s, %d)", fpr_str, private_only);
   2.189  
   2.190 -    sqlite3_stmt *stmt = private_only ? session->tsk_find : session->tpk_find;
   2.191 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_find : session->sq_sql.tpk_find;
   2.192      sqlite3_bind_text(stmt, 1, fpr_str, -1, SQLITE_STATIC);
   2.193  
   2.194      status = key_load(session, stmt, tpk, secret);
   2.195 @@ -542,7 +501,7 @@
   2.196      T("(%s, %d)", keyid_hex, private_only);
   2.197  
   2.198      sqlite3_stmt *stmt
   2.199 -        = private_only ? session->tsk_find_by_keyid : session->tpk_find_by_keyid;
   2.200 +        = private_only ? session->sq_sql.tsk_find_by_keyid : session->sq_sql.tpk_find_by_keyid;
   2.201      sqlite3_bind_text(stmt, 1, keyid_hex, -1, SQLITE_STATIC);
   2.202  
   2.203      status = key_load(session, stmt, tpkp, secretp);
   2.204 @@ -608,7 +567,7 @@
   2.205  static PEP_STATUS tpk_all(PEP_SESSION session, int private_only,
   2.206                            sq_tpk_t **tpksp, int *tpks_countp) {
   2.207      PEP_STATUS status = PEP_STATUS_OK;
   2.208 -    sqlite3_stmt *stmt = private_only ? session->tsk_all : session->tpk_all;
   2.209 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_all : session->sq_sql.tpk_all;
   2.210      status = key_loadn(session, stmt, tpksp, tpks_countp);
   2.211      ERROR_OUT(session, status, "loading TPKs");
   2.212   out:
   2.213 @@ -629,7 +588,7 @@
   2.214      T("(%s)", pattern);
   2.215  
   2.216      sqlite3_stmt *stmt
   2.217 -        = private_only ? session->tsk_find_by_email : session->tpk_find_by_email;
   2.218 +        = private_only ? session->sq_sql.tsk_find_by_email : session->sq_sql.tpk_find_by_email;
   2.219      sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_STATIC);
   2.220  
   2.221      status = key_loadn(session, stmt, tpksp, countp);
   2.222 @@ -690,7 +649,7 @@
   2.223  
   2.224  
   2.225      // Insert the TSK into the DB.
   2.226 -    sqlite3_stmt *stmt = session->begin_transaction;
   2.227 +    sqlite3_stmt *stmt = session->sq_sql.begin_transaction;
   2.228      int sqlite_result = sqlite3_step(stmt);
   2.229      sqlite3_reset(stmt);
   2.230      if (sqlite_result != SQLITE_DONE)
   2.231 @@ -698,7 +657,7 @@
   2.232                    "begin transaction failed: %s",
   2.233                    sqlite3_errmsg(session->key_db));
   2.234  
   2.235 -    stmt = session->tpk_save_insert_primary;
   2.236 +    stmt = session->sq_sql.tpk_save_insert_primary;
   2.237      sqlite3_bind_text(stmt, 1, fpr, -1, SQLITE_STATIC);
   2.238      sqlite3_bind_int(stmt, 2, is_tsk);
   2.239      sqlite3_bind_blob(stmt, 3, tsk_buffer, tsk_buffer_len, SQLITE_STATIC);
   2.240 @@ -710,7 +669,7 @@
   2.241                    "Saving TPK: %s", sqlite3_errmsg(session->key_db));
   2.242  
   2.243      // Insert the "subkeys" (the primary key and the subkeys).
   2.244 -    stmt = session->tpk_save_insert_subkeys;
   2.245 +    stmt = session->sq_sql.tpk_save_insert_subkeys;
   2.246      key_iter = sq_tpk_key_iter(tpk);
   2.247      sq_p_key_t key;
   2.248      while ((key = sq_tpk_key_iter_next(key_iter, NULL, NULL))) {
   2.249 @@ -733,7 +692,7 @@
   2.250      key_iter = NULL;
   2.251  
   2.252      // Insert the "userids".
   2.253 -    stmt = session->tpk_save_insert_userids;
   2.254 +    stmt = session->sq_sql.tpk_save_insert_userids;
   2.255      user_id_iter = sq_tpk_user_id_binding_iter(tpk);
   2.256      sq_user_id_binding_t binding;
   2.257      int first = 1;
   2.258 @@ -794,7 +753,8 @@
   2.259      if (! tried_commit) {
   2.260          tried_commit = 1;
   2.261          stmt = status == PEP_STATUS_OK
   2.262 -            ? session->commit_transaction : session->rollback_transaction;
   2.263 +            ? session->sq_sql.commit_transaction
   2.264 +            : session->sq_sql.rollback_transaction;
   2.265          int sqlite_result = sqlite3_step(stmt);
   2.266          sqlite3_reset(stmt);
   2.267          if (sqlite_result != SQLITE_DONE)