aboutsummaryrefslogtreecommitdiff
path: root/nix/libstore/sqlite.hh
blob: 326e4a4855b7d457657424812dd676bb0a47744f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once

#include <functional>
#include <string>

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

}
tc/guix-install.sh?id=3c69701f9735dd62a2f765b8bd23a7eaeb391412'>guix-install.sh: Add a trailing colon to 'INFOPATH'....Ludovic Courtès 2020-03-11guix-install.sh: Install SysV init script....Danny Milosavljevic 2020-01-26guix-install.sh: Correctly treat empty or unset $XDG_DATA_DIRS...Alexander Krotov 2020-01-26guix-install.sh: Create /etc/profile.d if it does not exist...Prafulla Giri 2020-01-26guix-install.sh: Export INFOPATH to contain updated guix info-pages...Prafulla Giri 2020-01-17guix-install.sh: Create an init profile during installation...Prafulla Giri 2019-12-04guix-install.sh: Use a deterministic umask....Tobias Geerinckx-Rice 2019-09-05guix-install.sh: Work around locale issues in 'guix-daemon.service'....Ludovic Courtès 2019-07-08guix-install.sh: Don't initialise ~/.gnupg....Tobias Geerinckx-Rice 2019-07-08guix-install.sh: Check for PGP key separately....Tobias Geerinckx-Rice 2019-07-08guix-install.sh: Add missing ‘local’ declaration....Tobias Geerinckx-Rice 2019-07-08guix-install.sh: Treat REQUIRE as the array it is....Tobias Geerinckx-Rice 2019-07-01etc: Do not recommend use of SKS key servers....Ricardo Wurmus 2019-06-29guix-install.sh: Don't authorise hydra.gnu.org....Tobias Geerinckx-Rice 2019-05-10etc: Refer to ci.guix.gnu.org in install script....Ricardo Wurmus 2019-05-02guix-install.sh: Update URL....Ludovic Courtès 2019-02-05guix-install.sh: Avoid "#!/bin/bash" shebang....Ludovic Courtès 2018-12-13guix-install.sh: Use pool.sks-keyservers.net....Ludovic Courtès 2018-12-05guix-install.sh: Use the "ci.guix.info.pub" file name for clarity....Ludovic Courtès 2018-11-23build: Binary tarball now populates the "current-guix" profile....Ludovic Courtès