aboutsummaryrefslogtreecommitdiff
#pragma once

#include <functional>
#include <string>
#include <cstdint>

#include "types.hh"

class sqlite3;
class sqlite3_stmt;

namespace nix {

/* RAII wrapper to close a SQLite database automatically. */
struct SQLite
{
    sqlite3 * db;
    SQLite() { db = 0; }
    ~SQLite();
    operator sqlite3 * () { return db; }
};

/* RAII wrapper to create and destroy SQLite prepared statements. */
struct SQLiteStmt
{
    sqlite3 * db = 0;
    sqlite3_stmt * stmt = 0;
    SQLiteStmt() { }
    void create(sqlite3 * db, const std::string & s);
    ~SQLiteStmt();
    operator sqlite3_stmt * () { return stmt; }

    /* Helper for binding / executing statements. */
    class Use
    {
        friend struct SQLiteStmt;
    private:
        SQLiteStmt & stmt;
        unsigned int curArg = 1;
        Use(SQLiteStmt & stmt);

    public:

        ~Use();

        /* Bind the next parameter. */
        Use & operator () (const std::string & value, bool notNull = true);
        Use & operator () (int64_t value, bool notNull = true);
        Use & bind(); // null

        int step();

        /* Execute a statement that does not return rows. */
        void exec();

        /* For statements that return 0 or more rows. Returns true iff
           a row is available. */
        bool next();

        std::string getStr(int col);
        int64_t getInt(int col);
    };

    Use use()
    {
        return Use(*this);
    }
};

/* RAII helper that ensures transactions are aborted unless explicitly
   committed. */
struct SQLiteTxn
{
    bool active = false;
    sqlite3 * db;

    SQLiteTxn(sqlite3 * db);

    void commit();

    ~SQLiteTxn();
};


MakeError(SQLiteError, Error);
MakeError(SQLiteBusy, SQLiteError);

[[noreturn]] void throwSQLiteError(sqlite3 * db, const format & f);

/* Convenience function for retrying a SQLite transaction when the
   database is busy. */
template<typename T>
T retrySQLite(std::function<T()> fun)
{
    while (true) {
        try {
            return fun();
        } catch (SQLiteBusy & e) {
        }
    }
}

}
hes>: Use it. Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com> Leo Prikler 2020-07-28gnu: webkitgtk: Update to 2.28.4....* gnu/packages/webkit.scm (webkitgtk): Update to 2.28.4. Mark H Weaver 2020-07-10gnu: webkitgtk: Update to 2.28.3 [security fixes]....Includes fixes for CVE-2020-9802, CVE-2020-9803, CVE-2020-9805, CVE-2020-9806, CVE-2020-9807, CVE-2020-9843, CVE-2020-9850, and CVE-2020-13753. * gnu/packages/webkit.scm (webkitgtk): Update to 2.28.3. Mark H Weaver 2020-06-02gnu: wpebackend-fdo: Update to 1.6.1....* gnu/packages/webkit.scm (wpebackend-fdo): Update to 1.6.1. Tobias Geerinckx-Rice 2020-05-06Merge branch 'master' into core-updatesMarius Bakke 2020-05-06gnu: webkitgtk: Patch to share store via Bubblewrap....Fixes <https://bugs.gnu.org/40837>. * gnu/packages/patches/webkitgtk-share-store.patch: New file. * gnu/local.mk (dist_patch_DATA): Add it. * gnu/packages/webkit.scm (webkitgtk)[source](patches): Use it. Co-authored-by: Marius Bakke <mbakke@fastmail.com> Jack Hill 2020-04-26Merge branch 'master' into core-updatesMarius Bakke 2020-04-24gnu: webkitgtk: Update to 2.28.2....* gnu/packages/webkit.scm (webkitgtk): Update to 2.28.2. Mark H Weaver 2020-04-15Merge branch 'master' into core-updatesMarius Bakke 2020-04-13gnu: webkitgtk: Update to 2.28.1....* gnu/packages/webkit.scm (webkitgtk): Update to 2.28.1. Mark H Weaver