wallet: refactor: dedup sqlite statement preparations/deletions #21540

pull theStack wants to merge 2 commits into bitcoin:master from theStack:2021-wallet-dedup_setupsqlstatements changing 1 files +33 −48
  1. theStack commented at 1:17 PM on March 28, 2021: member

    This refactoring PR deduplicates repeated SQLite statement preparation calls (sqlite3_prepare_v2(...)) / deletions (sqlite3_finalize(...)) and its surrounding logic by putting each prepared statement and its corresponding text representation into a ~std::map~ ~std::array~ std::vector. This should be more readable and less error-prone, e.g. in case an additional statement needs to be added in the future or the error handling has to be adapted.

  2. fanquake added the label Wallet on Mar 28, 2021
  3. fanquake added the label Refactoring on Mar 28, 2021
  4. S3RK commented at 6:36 AM on March 29, 2021: member

    Code review ACK 966089a

  5. Empact commented at 5:03 AM on March 30, 2021: member

    Concept ACK - the resulting code is significantly more readable IMO and this makes the consistent treatment obvious and enforced.

    Logically, the underlying values in statements are not associated as in a map (i.e. the indexing linking the values is not relevant - one is not logically a key and the other a value). A std::array<std::tuple> would seem to be more logically consistent.

  6. theStack force-pushed on Mar 30, 2021
  7. theStack commented at 11:50 AM on March 30, 2021: member

    Thanks for reviewing!

    Logically, the underlying values in statements are not associated as in a map (i.e. the indexing linking the values is not relevant - one is not logically a key and the other a value). A std::array<std::tuple> would seem to be more logically consistent.

    Agreed, using std::map here is like using a sledgehammer to crack a nut 😄 I changed the code to use std::array<std::pair<...,...>, N> now -- fortunately the loop code doesn't need to be changed at all. The only drawback is that we need to specify the size N of the array as second template parameter (std::make_array would help here, but that is not in the standard yet as far as I know), but I think we can live with that.

  8. S3RK commented at 7:04 AM on March 31, 2021: member

    reACK 0df8012. The only change is replacing map with array

  9. in src/wallet/sqlite.cpp:80 in 0df8012217 outdated
      99 | +    const std::array<std::pair<sqlite3_stmt**, const char*>, 5> statements {{
     100 | +        { &m_read_stmt,      "SELECT value FROM main WHERE key = ?" },
     101 | +        { &m_insert_stmt,    "INSERT INTO main VALUES(?, ?)" },
     102 | +        { &m_overwrite_stmt, "INSERT or REPLACE into main values(?, ?)" },
     103 | +        { &m_delete_stmt,    "DELETE FROM main WHERE key = ?" },
     104 | +        { &m_cursor_stmt,    "SELECT key, value FROM main" }
    


    MarcoFalke commented at 11:45 AM on April 2, 2021:
            { &m_cursor_stmt,    "SELECT key, value FROM main" },
    

    also could clang-format


    theStack commented at 1:58 PM on April 2, 2021:

    Thanks, done.

  10. in src/wallet/sqlite.cpp:75 in 0df8012217 outdated
      94 | -        }
      95 | -    }
      96 | -    if (!m_cursor_stmt) {
      97 | -        if ((res = sqlite3_prepare_v2(m_database.m_db, "SELECT key, value FROM main", -1, &m_cursor_stmt, nullptr)) != SQLITE_OK) {
      98 | -            throw std::runtime_error(strprintf("SQLiteDatabase: Failed to setup SQL statements : %s\n", sqlite3_errstr(res)));
      99 | +    const std::array<std::pair<sqlite3_stmt**, const char*>, 5> statements {{
    


    MarcoFalke commented at 11:45 AM on April 2, 2021:
        const std::vector<std::pair<sqlite3_stmt**, const char*>> statements {{
    

    You can use vector to avoid having to type the size


    theStack commented at 1:58 PM on April 2, 2021:

    Done.

  11. MarcoFalke commented at 11:46 AM on April 2, 2021: member

    Any reason to change this method, but not the Close method?

  12. wallet: refactor: dedup sqlite statement preparations 9a3670930e
  13. wallet: refactor: dedup sqlite statement deletions ea19cc844e
  14. theStack force-pushed on Apr 2, 2021
  15. theStack renamed this:
    wallet: refactor: dedup sqlite statement preparations
    wallet: refactor: dedup sqlite statement preparations/deletions
    on Apr 2, 2021
  16. theStack commented at 1:59 PM on April 2, 2021: member

    Took in the suggestions by MarcoFalke (https://github.com/bitcoin/bitcoin/pull/21540#pullrequestreview-626939176): applied clang-format, changed table type from std::array to std::vector to avoid typing the size and add another commit to also deduplicate the deletion of statements (sqlite3_finalize(...)). Thanks for reviewing!

  17. achow101 commented at 3:59 PM on April 2, 2021: member

    ACK ea19cc844e780b29825b26aee321204be981a3ae

  18. meshcollider commented at 5:00 AM on April 6, 2021: contributor

    utACK ea19cc844e780b29825b26aee321204be981a3ae

  19. fanquake commented at 6:17 AM on April 7, 2021: member

    It was while testing this I noticed #21628. However the issue is also present in master.

  20. fanquake merged this on Apr 7, 2021
  21. fanquake closed this on Apr 7, 2021

  22. sidhujag referenced this in commit ef8cd4cc48 on Apr 7, 2021
  23. theStack deleted the branch on Jul 31, 2021
  24. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me