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) {
        }
    }
}

}
ce to use p11-kit-next input instead of an implicit p11-kit from dependencies. Signed-off-by: Ludovic Courtès <ludo@gnu.org> John Kehayias 2021-10-25gnu: flatpak: Update to 1.12.1....* gnu/packages/package-management.scm (flatpak): Update to 1.12.1. Signed-off-by: Ludovic Courtès <ludo@gnu.org> John Kehayias 2021-10-18Merge remote-tracking branch 'signed/master' into core-updatesMathieu Othacehe 2021-10-17gnu: nix: Update to 2.3.16....* gnu/packages/package-management.scm (nix): Update to 2.3.16. Signed-off-by: Tobias Geerinckx-Rice <me@tobias.gr> jgart via Guix-patches via 2021-10-14gnu: guix: Update to c3c5028....* gnu/packages/package-management.scm (guix): Update to c3c5028. Ludovic Courtès 2021-10-14Merge branch 'master' into core-updates-frozenMarius Bakke 2021-10-12gnu: guix-build-coordinator: Update to 0-35.a4d0ec6....* gnu/packages/package-management.scm (guix-build-coordinator): Update to 0-35.a4d0ec6. Christopher Baines 2021-10-12gnu: Remove references to meson-0.55....This is a follow-up to 1eca979fb8da842e73c42f4f53be29b169810f2. * gnu/packages/datastructures.scm, gnu/packages/fontutils.scm, gnu/packages/terminals.scm: Don't import (gnu packages build-tools). * gnu/packages/wm.scm: Remove comment. * gnu/packages/emulators.scm (dosbox-staging)[arguments]: Remove meson field. * gnu/packages/package-management.scm (conan)[native-inputs]: Use meson. Efraim Flashner 2021-10-12Merge remote-tracking branch 'origin/master' into core-updates-frozen.Mathieu Othacehe 2021-10-08gnu: guix-build-coordinator: Update to 0-34.76d129d....* gnu/packages/package-management.scm (guix-build-coordinator): Update to 0-34.76d129d. Christopher Baines 2021-10-05gnu: guix: Update to 1.3.0-11.f743f20....* gnu/packages/package-management.scm (guix): Update to 1.3.0-11.f743f20. Mathieu Othacehe 2021-10-04gnu: guix: Update to 1.3.0-8.5fec148....* gnu/packages/package-management.scm (guix): Update to 1.3.0-8.5fec148. Tobias Geerinckx-Rice 2021-10-02gnu: Add guix-icons....* gnu/packages/package-management.scm (guix-icons): New variable. Mathieu Othacehe 2021-09-26gnu: Add conan....* gnu/packages/package-management.scm (conan): New variable. Maxim Cournoyer 2021-09-24gnu: guix: Update to ff77544....* gnu/packages/package-management.scm (guix): Update to ff77544. Tobias Geerinckx-Rice 2021-09-18gnu: guix: Update to 808f9ff....* gnu/packages/package-management.scm (guix): Update to 808f9ff. Ludovic Courtès 2021-09-07Merge branch 'master' into core-updates-frozenLudovic Courtès 2021-09-03gnu: libostree: Update to 2021.3....* gnu/packages/package-management.scm (libostree): Update to 2021.3. Tobias Geerinckx-Rice 2021-07-29Merge branch 'master' into core-updates-frozen... Conflicts: gnu/packages/bioinformatics.scm gnu/packages/cmake.scm gnu/packages/curl.scm gnu/packages/emacs-xyz.scm gnu/packages/gpodder.scm gnu/packages/music.scm gnu/packages/patches/glibc-bootstrap-system.patch gnu/packages/python-xyz.scm gnu/packages/shells.scm gnu/packages/statistics.scm Marius Bakke 2021-07-26gnu: guix-jupyter: Update to 0.2.2....* gnu/packages/package-management.scm (guix-jupyter): Update to 0.2.2. Ludovic Courtès 2021-07-24gnu: Use 'search-input-directory' for glibc locale data....* gnu/packages/gnome.scm (gnome-desktop)[arguments]: Use 'search-input-directory' when searching for glibc's "lib/locale". * gnu/packages/gnome.scm (geocode-glib): Likewise. * gnu/packages/package-management.scm (flatpak): Likewise. Ludovic Courtès 2021-07-23gnu: modules: Avoid FHS file names in 'add.modules'....* gnu/packages/package-management.scm (modules)[arguments]: Add 'patch-add-modules' phase. Ludovic Courtès 2021-07-23gnu: Add modules....* gnu/packages/package-management.scm (modules): New variable. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Ivan Gankevich